iosejabberdxmppframework

Get Roster List XMPP Framework


Following is the delegate method I have used and even though it gets called, 'queryElement' is assigned a nil value. Though the logged in user has many contacts.

-(BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
{
   NSXMLElement *queryElement = [iq elementForName: @"query" xmlns: @"jabber:iq:roster"];
   NSMutableArray *ArrayUsers = [[NSMutableArray alloc]init];
   if (queryElement) 
   {
       NSArray *itemElements = [queryElement elementsForName: @"item"];
       [ArrayUsers removeAllObjects];
       for (int i=0; i<[itemElements count]; i++) 
       {
           NSString *jid=[[[itemElements objectAtIndex:i] attributeForName:@"jid"] stringValue];
           [ArrayUsers addObject:jid];
       }
   }
   NSLog(@"users: %@",ArrayUsers);
   return NO;

}

Any one who could help getting buddy's list (both online and offline)?

XMPPFramework's method:

- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence

is called everytime a buddy comes online or goes offline. But a complete list of buddies is needed.


Solution

  • To get full roster list, you need to send a roster request:

    - (void)FetchFriends 
    {
        NSError *error = [[NSError alloc] init];
        NSXMLElement *query = [[NSXMLElement alloc] initWithXMLString:@"<query xmlns='jabber:iq:roster'/>"error:&error];
        NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
    
        [iq addAttributeWithName:@"type" stringValue:@"get"];
        [iq addAttributeWithName:@"id" stringValue:@"ANY_ID_NAME"];
        [iq addAttributeWithName:@"from" stringValue:@"ANY_ID_NAME@weejoob.info"];
        [iq addChild:query];
    
        [xmppStream sendElement:iq];
    }
    

    Server responds to request, and the response is treated by following delegate method:

    - (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
    {
        NSXMLElement *queryElement = [iq elementForName: @"query" xmlns: @"jabber:iq:roster"];
        if (queryElement) 
        {
            NSArray *itemElements = [queryElement elementsForName: @"item"];
            for (int i=0; i<[itemElements count]; i++)
            {
                NSLog(@"Friend: %@",[[itemElements[i] attributeForName:@"jid"]stringValue]);
            }
        }
        return NO;
    }
    

    The rosters may contain, type of relationship (to, none, both, from), based on your requirement you may choose the appropriate roster members by 'type' attribute of above item list.