Tiger
Tiger

Reputation: 310

How do I get a list of all online users in openfire/xmpp in iOS?

I am getting presence of user online / offline. this way if i need to check i have to send request every time to get presence

how would i able to check for multiple users or only online users ?

Upvotes: 2

Views: 1374

Answers (2)

Avijit Nagare
Avijit Nagare

Reputation: 8802

I have implement NSFetchedResultsControllerDelegate. i am getting list of Online user in "SectionNum"=0. whenever user goes offline/online controller's delegate method called.accordingly update tableView.

// NSFetchedResultsController *fetchedResultsController; //instance variable

in viewWillAppear

 //xmpp user array
    self.xmppUserArray=[[[self fetchedResultsController] fetchedObjects] mutableCopy];

    for (int i=0; i<[[self xmppUserArray] count]; i++) {

        if ([[[[self xmppUserArray] objectAtIndex:i] valueForKey:@"sectionNum"] integerValue]==0) {
            //this is user is online
            [[[AKSGetCareerGlobalClass SharedInstance] onlineUserArray] addObject:[[[self xmppUserArray] objectAtIndex:i] valueForKey:@"nickname"]];

        }
    }


//also implement method
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    //remove previous data or clear array

    [[self xmppUserArray] removeAllObjects];
    [[[AKSGetCareerGlobalClass SharedInstance] onlineUserArray] removeAllObjects];


    //get data from core data
    self.xmppUserArray=[[[self fetchedResultsController] fetchedObjects] mutableCopy];


    for (int i=0; i<[[self xmppUserArray] count]; i++) {

        if ([[[[self xmppUserArray] objectAtIndex:i] valueForKey:@"sectionNum"] integerValue]==0) {
            //this is user is online
            [[[AKSGetCareerGlobalClass SharedInstance] onlineUserArray] addObject:[[[self xmppUserArray] objectAtIndex:i] valueForKey:@"nickname"]];

        }
    }


    [[self msgTableView] reloadData];

}



-(NSFetchedResultsController *)fetchedResultsController {
    if (fetchedResultsController == nil)
    {
        NSManagedObjectContext *moc = [[self appDelegate] managedObjectContext_roster];

        NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorageObject"
                                                  inManagedObjectContext:moc];

        NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"sectionNum" ascending:YES];
        NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES];

        NSArray *sortDescriptors = [NSArray arrayWithObjects:sd1, sd2, nil];
        //NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES];

        //NSString *myJID = [[NSUserDefaults standardUserDefaults] stringForKey:@"userJID"];
        //NSLog(@"My JID ====>%@",myJID);

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"subscription=='both'"];//take care about subscription


        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        [fetchRequest setEntity:entity];
        [fetchRequest setPredicate:predicate];
        [fetchRequest setSortDescriptors:sortDescriptors];
        [fetchRequest setFetchBatchSize:20];

        fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                       managedObjectContext:moc
                                                                         sectionNameKeyPath:@"sectionNum"
                                                                                  cacheName:nil];
        [fetchedResultsController setDelegate:self];


        NSError *error = nil;
        if (![fetchedResultsController performFetch:&error])
        {
            DDLogError(@"Error performing fetch: %@", error);
        }

    }

    return fetchedResultsController;
}

Upvotes: 1

user2007547
user2007547

Reputation: 75

Use the online users plugin on the server. This way you can get the online users via http.

Here is the link : https://github.com/candy-chat/onlineUsersPlugin

Upvotes: 0

Related Questions