我试着这样:
XMPPRosterMemoryStorage *xmppRosterMemStorage = [[XMPPRosterMemoryStorage alloc] init];
XMPPRoster *xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:xmppRosterMemStorage
dispatchQueue:dispatch_get_main_queue()];
[xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue()];
xmppRoster.autoAcceptKnownPresenceSubscriptionRequests = true;
xmppRoster.autoFetchRoster = true;
[xmppRoster activate:xmppStream];
[xmppRoster fetchRoster];
但 xmppRoster
为空。为什么呢?
答案 0 :(得分:2)
名单项目存储在[xmppRosterMemStorage mainThreadManagedObjectContext]
中,您可以将其与NSFetchedResultsController
一起用作UITableView
的数据源,请参阅XMPPFramework来源中的iPhoneXMPP示例
答案 1 :(得分:1)
如果您不想使用NSFetchedResultsController
,则可以通过在名册存储对象上调用jidsForXMPPStream
来获取名册的JID。
以下是我如何使用它 -
- (void)xmppRosterDidEndPopulating:(XMPPRoster *)sender{
[appDelegate.mContactHandler clearContacts];
NSArray* jids = [mXmppRosterStorage jidsForXMPPStream:self.xmppStream];
for (int i=0; i<jids.count; i++)
{
RosterContact* contact = [[RosterContact alloc]init];
contact.jid = [jids objectAtIndex:i];
[appDelegate.mContactHandler addContact:contact];
}
[self postResultNotification:kReload withResult:nil];
}
我已经使用XMPPRoster的委托方法知道名单何时完成加载。然后我从存储中提取jid并创建我自己的RosterContact
对象,这只是我创建的用于存储联系人数据的自定义类。然后我发布通知重新加载我正在显示联系人列表的tableview。
这是我的名册宣言 -
mXmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] init];
mXmppRoster = [[XMPPRoster alloc] initWithRosterStorage:mXmppRosterStorage];
mXmppRoster.autoFetchRoster = YES;
mXmppRoster.autoAcceptKnownPresenceSubscriptionRequests = YES;
我希望这会有用。
答案 2 :(得分:0)
这是我对swift的解决方案。
https://stackoverflow.com/a/50151198/2781720
func getList() {
let query = try! XMLElement(xmlString: "<query xmlns='http://jabber.org/protocol/disco#items' node='all users'/>")
let iq = XMPPIQ(type: "get", to: XMPPJID(string: "Your Host Name"), elementID: xmppStream.generateUUID(), child: query)
iq?.addAttribute(withName: "id", stringValue: "get")
xmppStream.send(iq)
}
extension YourClassName: XMPPRosterDelegate {
func xmppRosterDidEndPopulating(_ sender: XMPPRoster!) {
if let jids = xmppRoster.xmppRosterStorage.jids(for: xmppStream) as? [XMPPJID] {
print("JIDS: \(String(describing: jids))")
for item in jids {
print(item.user)
}
}
}
}
希望这有帮助!