iOS XMPP framework get all registered users

后端 未结 3 1814
盖世英雄少女心
盖世英雄少女心 2020-12-09 23:53

In my chat application I want to get all online registered users. So everybody and not only people in my roster which is achieved with this code:

- (void)xmp         


        
相关标签:
3条回答
  • 2020-12-10 00:36
    - (void)getAllRegisteredUsers {
    
        NSError *error = [[NSError alloc] init];
        NSXMLElement *query = [[NSXMLElement alloc] initWithXMLString:@"<query xmlns='http://jabber.org/protocol/disco#items' node='all users'/>"
                                                                error:&error];
        XMPPIQ *iq = [XMPPIQ iqWithType:@"get"
                                     to:[XMPPJID jidWithString:@"DOMAIN"]
                              elementID:[xmppStream generateUUID] child:query];
        [xmppStream sendElement:iq];
    }
    
    - (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
    {
        NSXMLElement *queryElement = [iq elementForName: @"query" xmlns: @"http://jabber.org/protocol/disco#items"];
    
        if (queryElement) {
            NSArray *itemElements = [queryElement elementsForName: @"item"];
            NSMutableArray *mArray = [[NSMutableArray alloc] init];
            for (int i=0; i<[itemElements count]; i++) {
    
                NSString *jid=[[[itemElements objectAtIndex:i] attributeForName:@"jid"] stringValue];
                [mArray addObject:jid];
            }
    
    
    
        }
    
    0 讨论(0)
  • 2020-12-10 00:43

    I had the same issue, I got queryElement as nil as well. I've changed the response code to see the XML like this:

    - (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
    {
    //DDLogVerbose(@"%@: %@ - %@", THIS_FILE, THIS_METHOD, [iq elementID]);
    
    //NSXMLElement *queryElement = [iq elementForName:@"query" xmlns: @"http://jabber.org/protocol/disco#items"];
    NSXMLElement *queryElement = [iq elementForName:@"query" xmlns: @"jabber:iq:roster"];
    NSLog(@"IQ: %@",iq);
    if (queryElement) {
        NSArray *itemElements = [queryElement elementsForName: @"item"];
        NSMutableArray *mArray = [[NSMutableArray alloc] init];
        for (int i=0; i<[itemElements count]; i++) {
    
            NSString *jid=[[[itemElements objectAtIndex:i] attributeForName:@"jid"] stringValue];
            NSLog(@"%@",jid);
            [mArray addObject:jid];
        }
    }
    
    return NO; 
    }    
    

    As you may see what I've changed is the xmlns: from this xmlns: @"http://jabber.org/protocol/disco#items" to this xmlns: @"jabber:iq:roster" and that gave me the list of users.

    I'm using ejabberd, not sure if this works for all the others XMPP servers.

    Also I've found that this gave me the list of the "buddy" users, looks like if you want "all" users you need to make the query as an admin user. Please check this link for more information about it: https://www.ejabberd.im/node/3420

    0 讨论(0)
  • 2020-12-10 00:53

    After googling, You can not get all user easily, You must need to create Shared Roster Groups by follow step in the Example 1: everybody can see everybody else after done this you will get all the online users in the below delegate methods.

    - (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
    
    0 讨论(0)
提交回复
热议问题