如何使用XMPP框架在Facebook上列出仅在线用户

7
我已经在我的应用程序中集成了XMPP,并能够在表视图中列出所有用户,但我只想显示在线用户,然后想实现向我的在线好友发送和接收消息的功能...
请给我一些有用的代码建议...
这是我在Facebook登录后执行的代码。
    - (void)fbDidLogin
{
    NSLog(@"logged in.....................");
    [appDelegate.facebook requestWithGraphPath:@"me" andDelegate:self]; 

    DDLogVerbose(@"%s accessToken: %@ expirationDate: %@",__PRETTY_FUNCTION__,appDelegate.facebook.accessToken,appDelegate.facebook.expirationDate);
    self.accessToken = appDelegate.facebook.accessToken;

    if (xmppStreamFB) {  
        [xmppStreamFB release];  
        xmppStreamFB = nil;  
    }
    xmppStreamFB = [[XMPPStreamFacebook alloc] init];
    xmpReconnect = [[XMPPReconnect alloc] initWithStream:xmppStreamFB];  

    if (xmppRosterStorage) {  
        [xmppRosterStorage release];  
        xmppRosterStorage = nil;  
    }
    xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] init];

    if (xmppRoster) {  
        [xmppRoster release];  
        xmppRoster = nil;  
    } 
    xmppRoster = [[XMPPRoster alloc] initWithStream:xmppStreamFB rosterStorage:xmppRosterStorage];

    [xmppStreamFB addDelegate:self];
    [xmppRoster addDelegate:self];
    [xmppRoster setAutoRoster:YES];

    xmppStreamFB.myJID = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@chat.facebook.com", uid]];

    // You may need to alter these settings depending on the server you're connecting to
    allowSelfSignedCertificates = NO;
    allowSSLHostNameMismatch = YES;

    // Uncomment me when the proper information has been entered above.
    NSError *error = nil;
    if (![xmppStreamFB connect:&error]) 
        NSLog(@"Error connecting: %@", error);

    if(!tableView)
    {
        tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, 480, 320) style:UITableViewStylePlain];
    }
    [tableView setFrame:CGRectMake(0,0, 480, 320)];
    [tableView setTag:2];
    [tableView setDelegate:self];
    [tableView setDataSource:self];
    [tableView setHidden:NO];
    [tableView setBackgroundColor:[UIColor clearColor]];
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [tableView setAlpha:1.0];

    [self.view addSubview:tableView];

    [self.tableView reloadData];
    [self showTopBar];

}

我不知道XMPP框架显示用户在线并实现聊天功能的实际流程...

i have the following delegate methods as well..

    - (void)xmppStreamDidSecure:(XMPPStreamFacebook *)sender
{
    NSLog(@"---------- xmppStreamDidSecure: ----------");
}

- (void)xmppStreamDidConnect:(XMPPStreamFacebook *)sender
{
    NSLog(@"---------- xmppStreamDidConnect: ----------");

    isOpen = YES;

    NSError *error = nil;

    if (![self.xmppStreamFB authenticateWithAppId:_APP_ID accessToken:self.accessToken error:&error])
    {
        NSLog(@"Error authenticating: %@", error);
    }
    else {
        NSLog(@"NO Error authenticating:");
        /*
        ChatViewController *cvc = [[ChatViewController alloc] init];
        [self.view addSubview:cvc.view];*/
    }

}
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
    NSLog(@"---------- xmppStreamDidAuthenticate: ----------");

    [self goOnline];
}

- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error
{
    NSLog(@"---------- xmppStream:didNotAuthenticate: ----------");
}

- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
{
    NSLog(@"---------- xmppStream:didReceiveIQ: ----------");
    /*
    ChatViewController *cvc = [[ChatViewController alloc] init];
    [self.view addSubview:cvc.view];*/

    return NO;
}

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
    NSLog(@"---------- xmppStream:didReceiveMessage: ----------");
}

- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
{
    NSLog(@"---------- xmppStream:didReceivePresence: ----------");

}

- (void)xmppStream:(XMPPStream *)sender didReceiveError:(id)error
{
    NSLog(@"---------- xmppStream:didReceiveError: ----------");
}

- (void)xmppStreamDidDisconnect:(XMPPStream *)sender
{
    NSLog(@"---------- xmppStreamDidDisconnect: ----------");

    if (!isOpen)
    {
        NSLog(@"Unable to connect to server. Check xmppStream.hostName");
    }
}

还有两种在线和离线用户存在的方法,但是我不知道如何修改它们以适应我的任务:

    - (void)goOnline
{
    NSXMLElement *presence = [NSXMLElement elementWithName:@"presence"];

    [[self xmppStream] sendElement:presence];
}

- (void)goOffline
{
    NSXMLElement *presence = [NSXMLElement elementWithName:@"presence"];
    [presence addAttributeWithName:@"type" stringValue:@"unavailable"];

    [[self xmppStream] sendElement:presence];
}

确实非常有帮助:http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx - Jakub Hampl
你好,Rakesh。那是个好建议,但是managedObjectContext是什么?你能否给我们提供一段示例代码以便我们理解吗?先谢谢了。 - Apekshit
嗨,Rakesh,你能给我推荐一些关于Xmppframework的教程链接或演示吗?或者提供一些帮助。在authenticateWithPassword方法中,我遇到了一个错误信息:“summary string parsing error”。谢谢。 - Yogendra
嗨,Rakesh,我正在为Mac做同样的事情,authenticateWithPassword方法返回YES,但始终会调用didNotAuthenticate委托方法。你能给我推荐一些链接吗?谢谢。 - Yogendra
1个回答

7

经过许多努力,我终于找到了如何显示在线/离线/离开的用户。

我将逐步告诉您我是如何做到的,以便对经验较少的用户也有用。

第一步- 点击聊天按钮后,我调用以下方法 -

-(void) chatFacebook
{
   if (appDelegate.facebook == nil)
   {
     appDelegate.facebook = [[[Facebook alloc] initWithAppId:_APP_ID] autorelease];
   }

if (!accessToken)
{
    [appDelegate.facebook authorize:[XMPPStreamFacebook permissions] delegate:self appAuth:NO safariAuth:NO]; 
}
else
{
    [self fbDidLogin];
}

第二步- 现在是时候调用登录对话框委托方法了,如果登录成功,将调用fbDidLogin方法,以下是您应该包含的委托方法-

#pragma mark FBLoginDialogDelegate

/** * 当用户成功登录时调用。 */

- (void)fbDidLogin
{
NSLog(@"logged in.....................");
[appDelegate.facebook requestWithGraphPath:@"me" andDelegate:self]; 

DDLogVerbose(@"%s accessToken: %@ expirationDate: %@",__PRETTY_FUNCTION__,appDelegate.facebook.accessToken,appDelegate.facebook.expirationDate);
self.accessToken = appDelegate.facebook.accessToken;

if (xmppStream) {  
    [xmppStream release];  
    xmppStream = nil;  
}
xmppStream = [[XMPPStreamFacebook alloc] init];
xmpReconnect = [[XMPPReconnect alloc] initWithStream:xmppStream];  

if (xmppRosterStorage) {  
    [xmppRosterStorage release];  
    xmppRosterStorage = nil;  
}
xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] init];

if (xmppRoster) {  
    [xmppRoster release];  
    xmppRoster = nil;  
} 
xmppRoster = [[XMPPRoster alloc] initWithStream:xmppStream rosterStorage:xmppRosterStorage];

[xmppStream addDelegate:self];
[xmppRoster addDelegate:self];
[xmppRoster setAutoRoster:YES];

xmppStream.myJID = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@chat.facebook.com", uid]];

// You may need to alter these settings depending on the server you're connecting to
allowSelfSignedCertificates = NO;
allowSSLHostNameMismatch = YES;

// Uncomment me when the proper information has been entered above.
NSError *error = nil;
if (![xmppStream connect:&error]) 
    NSLog(@"Error connecting: %@", error);

if(!tableView)
{
    tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, 480, 320) style:UITableViewStylePlain];
}
[tableView setFrame:CGRectMake(0,0, 480, 320)];
[tableView setTag:2];
[tableView setDelegate:self];
[tableView setDataSource:self];
[tableView setHidden:NO];
[tableView setBackgroundColor:[UIColor clearColor]];
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[tableView setAlpha:1.0];

[self.view addSubview:tableView];

[self.tableView reloadData];
[self showTopBar];

/** * 当用户未登录而关闭对话框时调用。 */

  - (void)fbDidNotLogin:(BOOL)cancelled
  {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Canceled" message:@"Login cancled" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}

/** * 用户退出登录时调用。 */

  - (void)fbDidLogout
 {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Logged out" message:@"Logged out" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
  }

步骤3- fbDidLogin方法的第二行调用了FBRequestDelegate方法,因此您应该在.h类中包含此协议,以获取用户的uid(已登录,表示当前用户),您需要实现以下方法-

标记

标记FBRequestDelegate

  - (void)request:(FBRequest*)request didFailWithError:(NSError*)error{ 
     DDLogError(@"%s %@",__PRETTY_FUNCTION__,error); 
     //[appDelegate.facebook logout:self]; 
 } 

/** * 当请求返回并且其响应已解析为对象时调用。 * 生成的对象可能是字典、数组、字符串或数字,具体取决于API响应的格式。 */

  - (void)request:(FBRequest*)request didLoad:(id)result { 
    DDLogVerbose(@"%s............DDLOG................... %@",__PRETTY_FUNCTION__,result); 

NSLog(@" Result>>>>-------%@", result);

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:(NSMutableDictionary *)result];

uid = [dict objectForKey:@"id"];
NSLog(@"iddddddddddddd---%@", uid);

 } 

第四步- 现在是表视图的数据源和委托方法,您需要实现这些方法,以下是这些方法-
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView1
{
    return [[[self fetchedResultsController] sections] count];

// 需要实现NSFetchedResultsControllerDelegate }

- (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 @"";
 }


 - (NSInteger)tableView:(UITableView *)tableView1 numberOfRowsInSection:(NSInteger)sectionIndex
 {
    NSArray *sections = [[self fetchedResultsController] sections];

    if (sectionIndex < [sections count])
    {
        id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:sectionIndex];
        return sectionInfo.numberOfObjects;
        NSLog(@"section ifnfo ===========%@", sectionInfo);
    }   
}
return 0;
  }

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:CellIdentifier] autorelease];
    }

    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [cell setSelectionStyle:UITableViewCellSelectionStyleGray];

    user = [[self fetchedResultsController] objectAtIndexPath:indexPath];

    cell.textLabel.text = user.displayName;

    cell.textLabel.textColor = [UIColor whiteColor];


    cell1 = cell;

第五步-最后,您还需要实现NSFetchedResultsController委托方法,以便您可以使用聊天用户填充表格,以下是方法-

- (NSFetchedResultsController *)fetchedResultsController
{
if (fetchedResultsController == nil)
{
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorage"
                                              inManagedObjectContext:[self managedObjectContext]];

    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:[self managedObjectContext]
                                                                     sectionNameKeyPath:@"sectionNum"
                                                                              cacheName:nil];
    [fetchedResultsController setDelegate:self];

    [sd1 release];
    [sd2 release];
    [fetchRequest release];

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

return fetchedResultsController;
 }

  - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
 {
[[self tableView] reloadData];
 }

步骤6 - 编译并运行您的代码,用户列表应该以表格视图出现

如果出现任何问题,请分享,我随时可以帮助您。如果在发布此答案时出现一些错误,请不要介意,因为这是我第三次发布。

谢谢。


应该用 uid = [dict objectForKey:@"uid"]; 替换 uid = [dict objectForKey:@"id"]; - manutd
@Rakesh 我对Facebook API很熟悉,但对标准的XMPP协议还很新,最近在这个问题上发了一个帖子(http://stackoverflow.com/questions/27222641/fetch-online-presence-for-thousands-of-facebook-app-users)。上面的方法能有效地返回不互为好友的Facebook应用用户的在线状态吗?无论如何,我都很希望能得到你的指导,谢谢。 - user1544138

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接