在我的聊天应用程序中,我希望获得所有在线注册用户。所以每个人,而不仅仅是我的名单中的人,这是通过这个代码实现的:
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence {
// a buddy went offline/online
NSString *presenceType = [presence type]; // online/offline
NSString *myUsername = [[sender myJID] user];
NSString *presenceFromUser = [[presence from] user];
if (![presenceFromUser isEqualToString:myUsername]) {
if ([presenceType isEqualToString:@"available"]) {
[_chatDelegate newBuddyOnline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, @"chat.denederlandsewateren.nl"]];
} else if ([presenceType isEqualToString:@"unavailable"]) {
[_chatDelegate buddyWentOffline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, @"chat.denederlandsewateren.nl"]];
}
}
}
使用此代码,用户只能看到其他用户是朋友'但我需要在此特定域上注册的所有用户。这可能与ejabberd有关吗?
答案 0 :(得分:4)
- (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];
}
}
答案 1 :(得分:2)
我遇到了同样的问题,我也queryElement
为nil
。我已经更改了响应代码以查看 XML这样的内容:
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
{
//DDLogVerbose(@"%@: %@ - %@", THIS_FILE, THIS_METHOD, [iq elementID]);
//NSXMLElement *queryElement = [iq elementForName:@"query" xmlns: @"http://jabber.org/protocol/disco#items"];
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;
}
正如您可能会看到我已更改的内容是xmlns:
从此xmlns: @"http://jabber.org/protocol/disco#items"
到此xmlns: @"jabber:iq:roster"
,这给了我用户列表。
我正在使用ejabberd,不确定这是否适用于所有其他XMPP服务器。
此外,我发现这给了我“好友”用户的列表,看起来如果你想要“全部”用户,你需要以管理员用户的身份进行查询。请查看此链接以获取更多相关信息:https://www.ejabberd.im/node/3420
答案 2 :(得分:0)
点击后,您无法轻松获得所有用户,完成此操作后,您必须按照step in the Example 1: everybody can see everybody else创建共享名册组,您将获得以下代表中的所有在线用户方法
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence