获取名单列表XMPP框架

时间:2014-06-06 12:50:34

标签: ios ejabberd xmppframework

以下是我使用的委托方法,即使它被调用,' queryElement '被赋予 nil 值。虽然登录用户有很多联系人。

-(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;

}

任何能帮助获得好友列表(在线和离线)的人都可以?

XMPPFramework的方法:

- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
每当好友上线或下线时都会调用

。但是需要完整的好友列表。

2 个答案:

答案 0 :(得分:9)

要获得完整的名单,您需要发送一份名册请求:

- (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];
}

服务器响应请求,并通过以下委托方法处理响应:

- (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;
}

名单可能包含,关系类型(to,none,both,from),根据您的要求,您可以通过上面项目列表的'type'属性选择合适的名册成员。

答案 1 :(得分:0)

对于Swift 5:

func fetchFreind(){
        let query = try? DDXMLElement(xmlString:"<query xmlns='jabber:iq:roster'/>")
        let iq =  DDXMLElement(name: "iq")
        iq.addAttribute(withName:"type" , stringValue: "get")
        iq.addAttribute(withName:"id" , stringValue: "ANY_ID_NAME")
        iq.addAttribute(withName:"from" , stringValue: "ANY_ID_NAME@aaa")
        iq.addChild(query!)
        xmppStream.send(iq)
    }

您将获得列表:

 func xmppStream(_ sender: XMPPStream!, didReceive iq: XMPPIQ!) -> Bool {
        let queryElement = iq.elements(forName:"query")
        if queryElement.count > 0{
            let itemElement = queryElement[0].elements(forName: "item")
            for item in itemElement{
                print("User JID :- \(String(describing: item.attribute(forName: "jid")!))")
            }
        }
        return false
    }