您可以通过以下方式获取好友列表和其他详细信息:
像这样:
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
#import <FBSDKShareKit/FBSDKShareKit.h>
自定义 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)
{
NSLog(@"error is :%@",error);
}
else if (result.isCancelled)
{
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(@"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"]);
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];
注意:它只会显示那些下载了你的应用的朋友列表,而不是你Facebook朋友的所有朋友。根据新的API版本,无法获取朋友列表或朋友的电子邮件ID。