如何接收花名册的存在信息以及花名册列表



需要帮助获取好友的状态信息。

我正在调用"fetchRoster"函数,但是我正在获取花名册列表,而不是存在信息。

我也试着调用一个显式的状态信息。但是didRecievePresence委托在我的iOS应用中没有被调用。

问候,Cbhat

From Robbiehanson的XMPPFramework - RootViewController类:

  1. 取花名册

    - (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];
            NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
            [fetchRequest setEntity:entity];
            [fetchRequest setSortDescriptors:sortDescriptors];
            [fetchRequest setFetchBatchSize:10];
            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. 每当在服务器中记录到用户存在的更改时,重新加载您的表

    - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
    {
        [[self tableView] reloadData];
    }
    
  3. 显示头像(头像)

    - (void)configurePhotoForCell:(UITableViewCell *)cell user:(XMPPUserCoreDataStorageObject *)user
    {
        // Our xmppRosterStorage will cache photos as they arrive from the xmppvCardAvatarModule.
        // We only need to ask the avatar module for a photo, if the roster doesn't have it.
        if (user.photo != nil)
        {
            cell.imageView.image = user.photo;
        } 
        else
        {
            NSData *photoData = [[[self appDelegate] xmppvCardAvatarModule] photoDataForJID:user.jid];
            if (photoData != nil)
                cell.imageView.image = [UIImage imageWithData:photoData];
            else
                cell.imageView.image = [UIImage imageNamed:@"defaultPerson"];
        }
    }
    
  4. 将表格分成两部分—可用/离线

    - (NSString *)tableView:(UITableView *)sender titleForHeaderInSection:(NSInteger)sectionIndex
    {
        NSArray *sections = [[self fetchedResultsController] sections];
        if (sectionIndex < [sections count])
        {
            id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:sectionIndex];
            int section = [sectionInfo.name intValue];
            switch (section)
            {
                case 0  : return @"Available";
                case 1  : return @"Away";
                default : return @"Offline";
            }
        }
        return @"";
    }
    
  5. 用用户的显示名显示你的整个花名册

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                           reuseIdentifier:CellIdentifier];
        }
        XMPPUserCoreDataStorageObject *user = [[self fetchedResultsController] objectAtIndexPath:indexPath];
        cell.textLabel.text = user.displayName;
        [self configurePhotoForCell:cell user:user];
        return cell;
    }
    

这包含在您下载的XMPPFramework中。尝试一下。我说的这5点可能就是你需要的。

最新更新