2013-11-29 92 views

回答

5

嗨,大家好我終於找到了這個備選答案用自己的用戶名搜索用戶。

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

} 
+0

嗨anjaneyulu,謝謝你的回答,它幫了我很多。我在iq中獲得數據,現在我需要幫助來解析服務器的響應。我不知道該怎麼做。請幫幫我。 –

+0

我從服務器獲得了所有用戶。但不能從XML格式獲得數組。如果任何人都可以提供幫助。那麼請做好必要的事情,我會很樂意欣賞它 –

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]; 
    } 
} 
+0

嗨拉胡爾,我沒有得到任何註冊用戶誰在那裏服務器。可以請你說點什麼。 – Romance

+0

您使用的是正確的域名嗎? –

+0

我正在使用正確的域名,我的疑問是在服務器有20個用戶,並且一個用戶是我的朋友剩下19個用戶在服務器中有。我需要在服務器中的所有用戶,我不是問羣聊,我我問正常聊天,點對點。 – Romance

相關問題