iOS XMPP按用户名搜索用户

时间:2013-09-18 15:03:55

标签: iphone ios objective-c ipad xmpp

我第一次在iOS上使用XMPP Framework。任何人都可以使用他们的用户名搜索用户。

我尝试过以下链接中描述的方式。但没有运气。

iOS XMPP framework get all registered users

提前致谢。

2 个答案:

答案 0 :(得分:8)

我终于明白了。我做了自己。

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"]];

NSXMLElement *search = [NSXMLElement elementWithName:@"field"];
[search addAttributeWithName:@"var" stringValue:@"search"];
[search addChild:[NSXMLElement elementWithName:@"value" stringValue:searchField]];

[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.%@",[self appDelegate].hostName ]];
[iq addAttributeWithName:@"from" stringValue:userBare1];
[iq addChild:query];
[[[self appDelegate] xmppStream] sendElement:iq];

答案 1 :(得分:0)

正确答案。

以下代码

[iq addAttributeWithName:@"to" stringValue:[NSString stringWithFormat:@"search.%@",[self appDelegate].hostName ]];

表示

XMPPJID *myJID = [[[self appdelegate] xmppStream] myJID];
[iq addAttributeWithName:@"to" stringValue:[NSString stringWithFormat:@"search.%@",myJID.domain]];