iOS:通过分页获取Facebook好友

时间:2016-06-08 04:53:07

标签: ios iphone facebook

我正在尝试从Facebook上获取“facebook friends”列表,其中可能有超过500位朋友,因此Facebook对结果进行分页。这是方法。

- (void)getFaceBookFriends
{
if ([FBSDKAccessToken currentAccessToken])
{


    [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me/friends" parameters:@{@"fields" : @"first_name, last_name,email,name,gender,id,picture"}]
     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
         if (!error) {
             NSLog(@"%@",result);
             NSArray *arrData = [result valueForKey:@"data"];
             for (NSDictionary *dictItem in arrData) {
                 [array addObject:dictItem];
             }

             [tableView reloadData];
         }
         else
         {
         }
     }];
}
else
    [self checkFacebookSetup];
}

- (void)checkFacebookSetup
{

[self startLoadingActivity];
self.loginButton.publishPermissions = @[@"basic_info", @"email", @"user_friends"];
self.loginButton.delegate =self;
FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
loginManager.loginBehavior = FBSDKLoginBehaviorSystemAccount;
/*Editted*/
[loginManager logInWithReadPermissions:@[@"email"] fromViewController:(UIViewController *)self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
    if (error) {
        // Process error
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Access denied", nil) message:NSLocalizedString(@"You are not given access to your profile. enable it in settings", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil, nil];
        [alert show];
        [self stopLoadingActivity];
    } else if (result.isCancelled) {
        // Handle cancellations
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Access denied", nil) message:NSLocalizedString(@"You are not given access to your profile. enable it in settings", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil, nil];
        [alert show];
        [self stopLoadingActivity];
    } else {
        // If you ask for multiple permissions at once, you
        // should check if specific permissions missing
        if ([result.grantedPermissions containsObject:@"email"])
        {
            // Do work
            if ([FBSDKAccessToken currentAccessToken])
            {
                [self getFaceBookFriends];
            }
        }
    }
}];


}

1 个答案:

答案 0 :(得分:0)

如果您检查Facebook Developer guide,则需要发送限制param,请求如下:

[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me/friends?limit=25" parameters:@{@"fields" : @"first_name, last_name,email,name,gender,id,picture"}]

你会得到类似跟随的响应,所以你需要保存下一个分页游标并传递它

{
  "data": [
     ... Endpoint data is here
  ],
  "paging": {
    "cursors": {
      "after": "MTAxNTExOTQ1MjAwNzI5NDE=",
      "before": "NDMyNzQyODI3OTQw"
    },
    "previous": "https://graph.facebook.com/me/friends?limit=25&before=NDMyNzQyODI3OTQw"
    "next": "https://graph.facebook.com/me/friends?limit=25&after=MTAxNTExOTQ1MjAwNzI5NDE="
  }
}

之后,您需要在变量中保存after值,并使用以下代码再次调用下一个25数据的api:

  [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me/friends?limit=25&after=variableThatStoredAftervalue(MTAxNTExOTQ1MjAwNzI5NDE=)" parameters:@{@"fields" : @"first_name, last_name,email,name,gender,id,picture"}]

<强>更新

如果上述限制无效,请尝试以下代码检查facebook SDK class

 NSDictionary *parameters = @{
                                 @"fields": @"name",
                                 @"limit" : @"50"
                                 };
    // This will only return the list of friends who have this app installed
    FBSDKGraphRequest *friendsRequest = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me/friends"
                                                                          parameters:parameters];