【问题标题】:How do I get a list of all online users in openfire/xmpp in iOS?如何在 iOS 的 openfire/xmpp 中获取所有在线用户的列表?
【发布时间】:2014-11-06 07:58:27
【问题描述】:

我正在在线/离线获得用户的存在。这样,如果我需要检查,我必须每次都发送请求才能获得存在

如何检查多个用户或仅在线用户?

【问题讨论】:

    标签: ios xmpp openfire xmppframework


    【解决方案1】:

    我已经实现了 NSFetchedResultsControllerDelegate。我正进入(状态 “SectionNum”=0 中的在线用户列表。每当用户去 离线/在线控制器的委托方法调用。相应地更新 表格视图。

    // NSFetchedResultsController *fetchedResultsController; //实例变量

    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;
    }
    

    【讨论】:

      【解决方案2】:

      使用服务器上的在线用户插件。这样就可以通过http获取在线用户了。

      这是链接:https://github.com/candy-chat/onlineUsersPlugin

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-12-15
        • 2013-12-29
        • 1970-01-01
        • 2016-03-03
        • 2014-09-27
        • 2011-10-15
        • 1970-01-01
        相关资源
        最近更新 更多