2016-06-08 57 views
0

我想從Facebook獲取'Facebook朋友'列表,其中可能有超過500個朋友,因此Facebook會爲結果分頁。這是方法。iOS:使用分頁獲取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]; 
      } 
     } 
    } 
}]; 


} 

回答

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"}] 

UPDATE

如果再沒有工作限額以上試試下面的代碼檢查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];