Roster items are stored in [xmppRosterMemStorage mainThreadManagedObjectContext]
, you can use it with NSFetchedResultsController
as datasource for your UITableView
, see iPhoneXMPP example in the XMPPFramework sources
How to get friends from XMPPRoster?
-
25-06-2023 - |
Question
I try like this:
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];
But xmppRoster
is empty. Why?
Solution
OTHER TIPS
If you do not want to use NSFetchedResultsController
, you can get the JIDs of your roster from calling jidsForXMPPStream
on the roster storage object.
Here's how I have used it -
- (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];
}
I have used XMPPRoster's delegate method to know when the roster have finished loading. I then extract the jid from the storage and create my own RosterContact
object which is just a custom class I have created to store contact data. I then post a notification to reload the tableview where I am showing the contact list.
Here's my roster declaration -
mXmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] init];
mXmppRoster = [[XMPPRoster alloc] initWithRosterStorage:mXmppRosterStorage];
mXmppRoster.autoFetchRoster = YES;
mXmppRoster.autoAcceptKnownPresenceSubscriptionRequests = YES;
I hope this will be useful.
Here is my solution for 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)
}
}
}
}
Hope this helps!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow