Facebook iOS SDK – 在Graph API v2.4中获取好友列表

我使用下面的代码来获取使用app的Facebook好友

// Issue a Facebook Graph API request to get your user's friend list FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"/{friendlist-id}" parameters:nil HTTPMethod:@"GET"]; [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { // Handle the result }]; 

但我得到了以下回应。

 { data = ( ); summary = { "total_count" = 279; }; } 

我怎样才能获得fbID,就像我们使用FBRequest获取最后一个api版本一样。

注意我使用的是最新的FB SDK。

谢谢。

您必须在登录时添加user_friends权限

具有user_friends权限的用户访问令牌需要查看该人员的任何朋友列表。

你只能得到安装相同应用的朋友,

只有安装此应用程序的朋友才能在API v2.0及更高版本中返回。 total_count in summary表示朋友的总数,包括尚未安装该应用的朋友。

 FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me/friends" parameters:nil HTTPMethod:@"GET"]; [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { // Handle the result }]; 

根据新的API版本,它不可能获得朋友的FriendList或EmailId,但是如果你想要显示FriendList使用Facebook的TaggableFriendfunction。这个Taggable API需要来自Facebook的批准,这个API返回id,朋友的名字,个人资料图片URL。但是我不是FacebookId它在墙上的postID。这是获取朋友的唯一方式。但如果你使用我/朋友它返回朋友和朋友的总数app用户列表。 由于您没有应用程序用户,因此仅显示朋友总数。要实现此function,您需要使用管理员帐户(创建Facebook ID的帐户)登录,您必须实现分页概念。

使用此代码Spinnet:“/ me / tabggable_friends?fields = id,name,picture.type (large)。

你可以得到朋友列表和许多其他详细信息如下:

 #import  #import  #import  

自定义Facebook按钮单击:

 - (IBAction)facebook_click:(id)sender { FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init]; [login logInWithReadPermissions:@[@"public_profile", @"email", @"user_friends"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) { if (error) { // Process error NSLog(@"error is :%@",error); } else if (result.isCancelled) { // Handle cancellations NSLog(@"error is :%@",error); } else { if ([result.grantedPermissions containsObject:@"email"]) { NSLog(@"Login successfull"); [self fetchUserInfo]; [login logOut]; } } }]; } 

请求回复:

 -(void)fetchUserInfo { if ([FBSDKAccessToken currentAccessToken]) { [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id,name,link,first_name, last_name, picture.type(large), email, birthday,friends,gender,age_range,cover"}] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { if (!error) { //NSLog(@"result is :%@",result); NSLog(@"User ID : %@",[result valueForKey:@"id"]); NSLog(@"User Name : %@",[result valueForKey:@"name"]); NSLog(@"User First Name :%@",[result valueForKey:@"first_name"]); NSLog(@"User Last Name :%@",[result valueForKey:@"last_name"]); NSLog(@"USER Email is :%@",[result valueForKey:@"email"]); NSLog(@"User fb_Link : %@",[result valueForKey:@"link"]); NSLog(@"User Birthday : %@",[result valueForKey:@"birthday"]); NSLog(@"FB Profile Photo Link :%@",[[[result valueForKey:@"picture"]objectForKey:@"data"]objectForKey:@"url"]); NSLog(@"User total friends : %@",[[[result valueForKey:@"friends"]objectForKey:@"summary"]valueForKey:@"total_count"]); NSLog(@"User Gender : %@",[result valueForKey:@"gender"]); NSLog(@"User age_range : %@",[[result valueForKey:@"age_range"]objectForKey:@"min"]); NSLog(@"User cover Photo Link : %@",[[result valueForKey:@"cover"]objectForKey:@"source"]); //Friend List ID And Name NSArray * allKeys = [[result valueForKey:@"friends"]objectForKey:@"data"]; fb_friend_Name = [[NSMutableArray alloc]init]; fb_friend_id = [[NSMutableArray alloc]init]; for (int i=0; i<[allKeys count]; i++) { [fb_friend_Name addObject:[[[[result valueForKey:@"friends"]objectForKey:@"data"] objectAtIndex:i] valueForKey:@"name"]]; [fb_friend_id addObject:[[[[result valueForKey:@"friends"]objectForKey:@"data"] objectAtIndex:i] valueForKey:@"id"]]; } NSLog(@"Friends ID : %@",fb_friend_id); NSLog(@"Friends Name : %@",fb_friend_Name); } }]; } } 

您可以使用此API获取朋友资料图片。

 NSString *str = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?type=large",friend_id]; NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]]; UIImage *profilePic = [UIImage imageWithData:imageData]; 

注意:它只会显示下载您的应用程序的好友列表。 不是你朋友的所有朋友。 根据新的API版本,它无法获得FriendList或EmailId of Friends