使用Openfire Server的iOS XMPP Framework可以获得所有注册用户

时间:2013-11-29 06:19:51

标签: ios objective-c openfire xmppframework

我的 IOS 聊天应用程序我正在使用 Openfire 服务器,我正在检索特定用户好友列表,但现在我想从 Openfire <检索所有注册用户/ strong>服务器。请帮助我,并提前感谢。

2 个答案:

答案 0 :(得分:6)

嗨,大家终于通过使用用户名搜索用户找到了替代答案。

1.对于每个联系人我都会像这样添加emailid username @ domainname  2.在地方搜索字符串中,如果我们使用域名,那么我们将获得所有注册用户。

//Request
- (void)getDetailsofRegisteredUser {

    //To Search Peticular User either by using their name, email or username
    NSString *userBare1  = [[[[self appDelegate] xmppStream] myJID] bare];
    NSXMLElement *query = [NSXMLElement elementWithName:@"query"];
    [query addAttributeWithName:@"xmlns" stringValue:@"jabber:iq:search"];

    NSXMLElement *x = [NSXMLElement elementWithName:@"x" xmlns:@"jabber:x:data"];
    [x addAttributeWithName:@"type" stringValue:@"submit"];

    NSXMLElement *formType = [NSXMLElement elementWithName:@"field"];
    [formType addAttributeWithName:@"type" stringValue:@"hidden"];
    [formType addAttributeWithName:@"var" stringValue:@"FORM_TYPE"];
    [formType addChild:[NSXMLElement elementWithName:@"value" stringValue:@"jabber:iq:search" ]];

    NSXMLElement *userName = [NSXMLElement elementWithName:@"field"];
    [userName addAttributeWithName:@"var" stringValue:@"Username"];
    [userName addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1" ]];

    NSXMLElement *name = [NSXMLElement elementWithName:@"field"];
    [name addAttributeWithName:@"var" stringValue:@"Name"];
    [name addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1"]];

    NSXMLElement *email = [NSXMLElement elementWithName:@"field"];
    [email addAttributeWithName:@"var" stringValue:@"Email"];
    [email addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1"]];

    //Here in the place of SearchString we have to provide registered user name or emailid or username(if it matches in Server it provide registered user details otherwise Server provides response as empty)
    NSXMLElement *search = [NSXMLElement elementWithName:@"field"];
    [search addAttributeWithName:@"var" stringValue:@"search"];
    [search addChild:[NSXMLElement elementWithName:@"value" stringValue:[NSString stringWithFormat:@"%@", SearchString]]];

    [x addChild:formType];
    [x addChild:userName];
    [x addChild:name];
    [x addChild:email];
    [x addChild:search];
    [query addChild:x];

    NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
    [iq addAttributeWithName:@"type" stringValue:@"set"];
    [iq addAttributeWithName:@"id" stringValue:@"searchByUserName"];
    [iq addAttributeWithName:@"to" stringValue:[NSString stringWithFormat:@"search.%@",DOMAIN_NAME]];
    [iq addAttributeWithName:@"from" stringValue:userBare1];
    [iq addChild:query];
    [[[self appDelegate] xmppStream] sendElement:iq];

}

//We will get response here

- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
{

    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
    NSLog(@"searched format %@", iq);

}

答案 1 :(得分:0)

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