在我的应用程序中,我需要从 OpenFire 获取所有用户名。
我使用下面的代码,但它没有提供nil数据。
我的 didReceiveIQ 方法出了点问题但是首次使用XMPP,所以不能解决实际问题。
我的获取数据的代码是
- (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:[NSString stringWithFormat:@"Domain Name with jid"]]
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"];
NSLog(@"queryelement %@",queryElement);
if (queryElement) {
NSLog(@"query in if");
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];
NSLog(@"User in OpenFire = %@",jid);
}
NSLog(@"mArray = %@",mArray);
}
return NO;
}
赞赏任何形式的帮助......
答案 0 :(得分:2)
我正在努力让所有注册用户从openfire进入我的应用程序。我尝试了以上所有方法,但没有帮助我! 经过多次尝试后,我得到了一个获得所有注册用户的解决方案...... 这是代码:
- (void)getAllRegisteredUsers {
NSError *error = [[NSError alloc] init];
NSXMLElement *query = [[NSXMLElement alloc] initWithXMLString:@"<query xmlns='jabber:iq:roster'/>" error:&error];
XMPPIQ *iq = [DDXMLElement elementWithName:@"iq"];
[iq addAttributeWithName:@"type" stringValue:@"get"];
[iq addAttributeWithName:@"none" stringValue:@"ANY_ID_NAME"];
[iq addAttributeWithName:@"both" stringValue:@"ANY_ID_NAME"];
[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"];
NSMutableArray *mArray = [[NSMutableArray alloc] init];
for (int i=0; i<[itemElements count]; i++) {
NSString *jid=[[[itemElements objectAtIndex:i] attributeForName:@"jid"] stringValue];
[mArray addObject:jid];
}
NSLog(@"%@",mArray);
}
return YES;
}
这对我有用,希望它也适用于其他人...... :)
答案 1 :(得分:0)
通过此代码,您可以获得名单(来自开放式服务器的朋友列表)。
NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns:@"jabber:iq:roster"];
NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
[iq addAttributeWithName:@"type" stringValue:@"get"];
[iq addChild:query];
[xmppStream sendElement:iq];
响应将与您期望的相同。
答案 2 :(得分:0)
尝试这样的事情:
[xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue() ] ;
[xmppRoster fetchRoster ] ;
你应该在获取名单之前初始化XMPPRoster
:
xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] init];
// xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] initWithInMemoryStore];
xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:xmppRosterStorage];
在fetchRoster
对象上发送XMPPRoster
后,您可以从中获取所有用户名
XMPPRosterDelegate
的方法:
- (void)xmppRoster:(XMPPRoster *)sender didReceiveRosterItem:(NSXMLElement *)item
希望这可以提供帮助:]
答案 3 :(得分:0)
我有同样的问题,我也将queryElement作为nil。我已经更改了响应代码,以便像这样查看XML:
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
{
NSXMLElement *queryElement = [iq elementForName:@"query" xmlns: @"jabber:iq:roster"];
NSLog(@"IQ: %@",iq);
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];
NSLog(@"%@",jid);
[mArray addObject:jid];
}
}
return NO;
}