iOS:通过分页获取 Facebook 好友
Posted
技术标签:
【中文标题】iOS:通过分页获取 Facebook 好友【英文标题】:iOS: Fetch Facebook friends with pagination 【发布时间】:2016-06-08 04:53:06 【问题描述】:我正在尝试从 Facebook 获取“facebook 朋友”列表,其中可能有 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】:如果您检查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
值保存在变量中,然后使用以下代码再次调用 api 以获取接下来的 25 个数据:
[[[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];
【讨论】:
以上是关于iOS:通过分页获取 Facebook 好友的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 FBSDK for iOS 获取 Facebook 好友列表?