简体   繁体   中英

iOS: Fetch Facebook friends with pagination

I am trying to fetch 'facebook friends' list from Facebook, where there may be more than 500 friends, so Facebook paginates the results. Here is the method.

- (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];
            }
        }
    }
}];


}

If you check the Facebook Developer guide you need to send limit param with request like:

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

And you will get response something like following so you need to save next next paging cursors and pass it like

{
  "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 that you need to save after value in variable and call again the api for next 25 data using following code :

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

UPDATE

If above limit not working then try the following code check 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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM