关于Objective C:如何从Facebook iOS SDK获取朋友列表

How to get Friends list from Facebook iOS sdk

我正在使用iOS sdk v3.18.1,我想让我所有的Facebook朋友都可以得到我的朋友计数,但是数据为零。

这是我的代码

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

输出

1
2
3
4
5
6
7
{
    data =     (
    );
    summary =     {
       "total_count" = 840;
    };
}


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

自v2.0起,您将无法再获得完整的朋友列表,因此您也只会获得对您的应用进行授权的朋友。

也在此主题中查看我的答案:如何获取所有用户朋友的列表(不仅是使用该应用程序的用户)?


1
2
3
4
5
// 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朋友列表。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 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的限制),然后将其传递给参数。

1
////////////////////////////////////////////////////////////////////////

对于非应用程序朋友-

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

同样,要结交非应用程序朋友,您需要使用(/ me / invitable_friends),如下所示-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
- (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图形api返回的朋友列表的邀请令牌。您可以将这些邀请令牌与FBWebDialogs一起使用,以将邀请发送给朋友,如下所示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
- (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);
                 }
             }
         }
     }];
}

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

4月之后的所有Facebook SDK版本都拒绝此功能来获取整个好友列表。

参考:所以问题:Facebook图形API返回空...。FACEBOOK用户指南

这已被FACEBOOK确认。