使用FacebookSDK获取Facebooklogin用户的朋友列表

我想在我的iPhone应用程序中使用FacebookSDK访问朋友列表和他们的个人资料图片。 请build议演示或代码。

FBRequest* friendsRequest = [FBRequest requestForMyFriends]; [friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection, NSDictionary* result, NSError *error) { NSArray* friends = [result objectForKey:@"data"]; NSLog(@"Found: %i friends", friends.count); for (NSDictionary<FBGraphUser>* friend in friends) { NSLog(@"I have a friend named %@ with id %@", friend.name, friend.id); } }]; 

您需要使用FBRequest,requestForMyFriends方法,它会使用活动会话创build一个表示对“我/朋友”端点进行Graph API调用的请求。

 FBRequest* friendsRequest = [FBRequest requestForMyFriends]; [friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection, NSDictionary* result, NSError *error) { NSArray* friends = [result objectForKey:@"data"]; NSLog(@"Found: %i friends", friends.count); for (NSDictionary<FBGraphUser>* friend in friends) { NSLog(@"My friend named %@ and id %@", friend.name, friend.id); } }]; 
 NSArray *permissions = [[NSArray alloc] initWithObjects: @"user_birthday",@"friends_hometown", @"friends_birthday",@"friends_location", nil]; [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler:write handler code..] 
 [FBRequestConnection startForMyFriendsWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary* result, NSError *error) { NSArray *data_array = [result objectForKey:@"data"]; //array of friends NSMutableArray *pictures = [NSMutableArray array]; // array for pictures for (NSDictionary *friend in data_array) { NSString *friend_id = friend[@"id"]; NSString *urlString = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture",friend_id]; NSURL *url = [NSURL urlWithString:urlString]; [pictures addObject:[NSData dataWithContentsOfURL:url]]; } }]; 

由于[NSData dataWithContentsOfURL:url]相当慢,你可以考虑在后台运行这个代码