【问题标题】:How to get Friends list from Facebook iOS sdk如何从 Facebook iOS sdk 获取好友列表
【发布时间】:2014-09-29 09:03:05
【问题描述】:

我正在使用 iOS sdk v3.18.1,我想获取我所有的 Facebook 好友。我可以获取好友数,但是数据为零。

这是我的代码

[FBRequestConnection startWithGraphPath:@"me/friends" parameters:nil HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
        NSLog(@"result %@",result);
    }];

输出

{
    data =     (
    );
    summary =     {
        "total_count" = 840;
    };
}

【问题讨论】:

标签: ios objective-c iphone facebook-graph-api


【解决方案1】:

https://developers.facebook.com/docs/apps/changelog

从 v2.0 开始,您无法获取完整的好友列表,您只能获取授权您的应用的好友。

也可以在此线程中查看我的答案:how to get a list of all user friends (not only who use the app)?

【讨论】:

    【解决方案2】:
    // declare an array in header file which will hold the list of all friends - 
    NSMutableArray * m_allFriends;
    
    // alloc and initialize the array only once 
    m_allFriends = [[NSMutableArray alloc] init];
    

    使用 FB SDK 3.0 和 2.0 以上的 API 版本,您需要调用以下函数(与我/朋友的图形 api)来获取使用相同应用程序的 FB 朋友列表。

    // get friends which use the app
    
    -(void) getMineFriends
    {
        [FBRequestConnection startWithGraphPath:@"me/friends"
                                     parameters:nil
                                     HTTPMethod:@"GET"
                              completionHandler:^(
                                                  FBRequestConnection *connection,
                                                  id result,
                                                  NSError *error
                                                  ) {
                                  NSLog(@"me/friends result=%@",result);
    
                                  NSLog(@"me/friends error = %@", error.description);
    
                                  NSArray *friendList = [result objectForKey:@"data"];
    
                                  [m_allFriends addObjectsFromArray: friendList];
                              }];
    }
    

    注意 : 1) 上述查询返回的好友数量默认限制为 25。 2) 如果结果中有下一个链接,则表示您将在下一个查询中获取更多好友,依此类推. 3)或者,您可以更改限制(减少限制,超过 25 的限制)并在参数中传递。

    ////////////////////////////////////////////////////////////////////////
    

    对于非应用好友 -

    // m_invitableFriends - global array which will hold the list of invitable friends
    

    还要获得非应用好友,您需要使用 (/me/invitable_friends) 如下 -

    - (void) getAllInvitableFriends
    {
        NSMutableArray *tempFriendsList =  [[NSMutableArray alloc] init];
        NSDictionary *limitParam = [NSDictionary dictionaryWithObjectsAndKeys:@"100", @"limit", nil];
        [self getAllInvitableFriendsFromFB:limitParam addInList:tempFriendsList];
    }
    
    - (void) getAllInvitableFriendsFromFB:(NSDictionary*)parameters
                                addInList:(NSMutableArray *)tempFriendsList
    {
        [FBRequestConnection startWithGraphPath:@"/me/invitable_friends"
                                     parameters:parameters
                                     HTTPMethod:@"GET"
                              completionHandler:^(
                                                  FBRequestConnection *connection,
                                                  id result,
                                                  NSError *error
                                                  ) {
                                  NSLog(@"error=%@",error);
    
                                  NSLog(@"result=%@",result);
    
                                  NSArray *friendArray = [result objectForKey:@"data"];
    
                                  [tempFriendsList addObjectsFromArray:friendArray];
    
                                  NSDictionary *paging = [result objectForKey:@"paging"];
                                  NSString *next = nil;
                                  next = [paging objectForKey:@"next"];
                                  if(next != nil)
                                  {
                                      NSDictionary *cursor = [paging objectForKey:@"cursors"];
                                      NSString *after = [cursor objectForKey:@"after"];
                                      //NSString *before = [cursor objectForKey:@"before"];
                                      NSDictionary *limitParam = [NSDictionary dictionaryWithObjectsAndKeys:
                                                                  @"100", @"limit", after, @"after"
                                                                  , nil
                                                                  ];
                                      [self getAllInvitableFriendsFromFB:limitParam addInList:tempFriendsList];
                                  }
                                  else
                                  {
                                      [self replaceGlobalListWithRecentData:tempFriendsList];
                                  }
                              }];
    }
    
    - (void) replaceGlobalListWithRecentData:(NSMutableArray *)tempFriendsList
    {
        // replace global from received list
        [m_invitableFriends removeAllObjects];
        [m_invitableFriends addObjectsFromArray:tempFriendsList];
        //NSLog(@"friendsList = %d", [m_invitableFriends count]);
        [tempFriendsList release];
    }
    

    用于邀请非应用好友 -

    您将获得带有我/invitable_friends graph api返回的朋友列表的邀请令牌。您可以将这些邀请令牌与 FBWebDialogs 一起使用,如下所示向朋友发送邀请

    - (void) openFacebookFeedDialogForFriend:(NSString *)userInviteTokens {
    
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                       userInviteTokens, @"to",
                                       nil, @"object_id",
                                       @"send", @"action_type",
                                       actionLinksStr, @"actions",
                                       nil];
    
        [FBWebDialogs
         presentRequestsDialogModallyWithSession:nil
         message:@"Hi friend, I am playing game. Come and play this awesome game with me."
         title:nil
         parameters:params
         handler:^(
                   FBWebDialogResult result,
                   NSURL *url,
                   NSError *error)
         {
             if (error) {
                 // Error launching the dialog or sending the request.
                 NSLog(@"Error sending request : %@", error.description);
             }
             else
             {
                 if (result == FBWebDialogResultDialogNotCompleted)
                 {
                     // User clicked the "x" icon
                     NSLog(@"User canceled request.");
                     NSLog(@"Friend post dialog not complete, error: %@", error.description);
                 }
                 else
                 {
                     NSDictionary *resultParams = [g_mainApp->m_appDelegate parseURLParams:[url query]];
    
                     if (![resultParams valueForKey:@"request"])
                     {
                         // User clicked the Cancel button
                         NSLog(@"User canceled request.");
                     }
                     else
                     {
                         NSString *requestID = [resultParams valueForKey:@"request"];
    
                         // here you will get the fb id of the friend you invited,
                         // you can use this id to reward the sender when receiver accepts the request
    
                         NSLog(@"Feed post ID: %@", requestID);
                         NSLog(@"Friend post dialog complete: %@", url);
                     }
                 }
             }
         }];
    }
    

    【讨论】:

      【解决方案3】:

      自 Graph API V2.0 起,您将只能获取与您的应用关联的好友列表。在 Graph API v2.0 中,调用 /me/friends 会返回该人使用该应用程序的朋友。是的,可以获取计数,但无法访问好友列表。

      4 月之后发布的所有 Facebook SDK 都拒绝提供获取整个好友列表的功能。

      参考:SO QUESTION:Facebook graph API returns empty ....FACEBOOK USER GUIDE

      This has been confirmed by FACEBOOK

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-05
        相关资源
        最近更新 更多