如何从XMPPRoster获得朋友?

时间:2014-04-01 08:49:31

标签: ios xmpp xmppframework

我试着这样:

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 为空。为什么呢?

3 个答案:

答案 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)
       }
     }
   }
 }

希望这有帮助!