在iOS中从Facebook获取用户的个人信息

Objective-C和iPhone开发环境相当新颖。

我正在执行Facebooklogin在我的应用程序获取用户的名称,电子邮件和个人资料图片。 我已成功实施login部分,并已收到人的姓名和用户名。

现在我想从Facebook获取用户的电子邮件和个人资料图片,但我没有任何想法如何得到它。我使用Facebook IOS SDK v4.0。

当我有用户名时,如何从Facebook获取用户的个人资料图片和电子邮件ID?

要获取用户电子邮件ID,您必须在login时请求电子邮件许可。

FBSDKLoginButton *loginView = [[FBSDKLoginButton alloc] init]; loginView.readPermissions = @[@"email"]; loginView.frame = CGRectMake(100, 150, 100, 40); [self.view addSubview:loginView]; 

您可以使用GraphPath在New SDK中获取用户电子邮件ID。

 [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { if (!error) { NSLog(@"fetched user:%@ and Email : %@", result,result[@"email"]); } }]; } 

结果会让你所有的用户详细信息和结果[@“电子邮件”]会让你的电子邮件login用户。

要获取个人资料图片,您可以使用

 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=normal",result[@"id"]]]; NSData *data = [NSData dataWithContentsOfURL:url]; _imageView.image = [UIImage imageWithData:data]; 

或者你也可以使用FBSDKProfilePictureView来获取个人资料图片通过传递用户个人资料ID:

 FBSDKProfilePictureView *profilePictureview = [[FBSDKProfilePictureView alloc]initWithFrame:_imageView.frame]; [profilePictureview setProfileID:result[@"id"]]; [self.view addSubview:profilePictureview]; 

请参阅: https : //developers.facebook.com/docs/facebook-login/ios/v2.3#profile_picture_view

或者你也可以通过传递参数

 [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"picture, email"}] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { if (!error) { NSString *pictureURL = [NSString stringWithFormat:@"%@",[result objectForKey:@"picture"]]; NSLog(@"email is %@", [result objectForKey:@"email"]); NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:pictureURL]]; _imageView.image = [UIImage imageWithData:data]; } else{ NSLog(@"%@", [error localizedDescription]); } }]; 

对不起,这个混乱的答案,这是我有史以来第一个答案。 您可以使用FBSDK Graph请求获取用户的所有configuration文件信息和FBSDKProfilePictureView类来轻松获取用户的configuration文件图片。此代码用于手动FacebookloginUI。

首先,你必须把这个代码放在login过程开始的地方:

  FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init]; [login logInWithReadPermissions:@[@"public_profile", @"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) { if (error) { // There is an error here. } else { if(result.token) // This means if There is current access token. { // Token created successfully and you are ready to get profile info [self getFacebookProfileInfo]; } } }]; 

如果login成功,则执行此方法获取用户的公共configuration文件;

 -(void)getFacebookProfileInfos { FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc]initWithGraphPath:@"me" parameters:nil]; FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init]; [connection addRequest:requestMe completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { if(result) { if ([result objectForKey:@"email"]) { NSLog(@"Email: %@",[result objectForKey:@"email"]); } if ([result objectForKey:@"first_name"]) { NSLog(@"First Name : %@",[result objectForKey:@"first_name"]); } if ([result objectForKey:@"id"]) { NSLog(@"User id : %@",[result objectForKey:@"id"]); } } }]; [connection start]; 

获取当前login用户的个人资料图片:

 FBSDKProfilePictureView *pictureView=[[FBSDKProfilePictureView alloc]init]; [pictureView setProfileID:@"user_id"]; [pictureView setPictureMode:FBSDKProfilePictureModeSquare]; [self.view addSubview:pictureView]; 

您必须添加刷新代码到您的viewDidLoad方法:

  [FBSDKProfile enableUpdatesOnAccessTokenChange:YES]; 
 Hope This could Help You .. - (IBAction)Loginwithfacebookaction:(id)sender { FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init]; [login logOut]; [login logInWithReadPermissions:@[@"public_profile"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) { if (error) { NSLog(@"Process error"); } else if (result.isCancelled) { NSLog(@"Cancelled"); } else { [self getFacebookProfileInfos]; } }]; } - (void)finishedWithAuth: (GTMOAuth2Authentication *)auth error: (NSError *) error { NSLog(@"Received error %@ and auth object %@",error, auth); if (!error) { email =signIn.userEmail; [[NSUserDefaults standardUserDefaults] setObject:email forKey:@"useremail"]; NSLog(@"Received error and auth object %@",signIn.userEmail); NSLog(@"Received error and auth object %@",signIn.userID); if ( auth.userEmail) { [[[GPPSignIn sharedInstance] plusService] executeQuery:[GTLQueryPlus queryForPeopleGetWithUserId:@"me"] completionHandler:^(GTLServiceTicket *ticket, GTLPlusPerson *person, NSError *error) { // this is for fetch profile image NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",person.image.url]]; NSLog(@"%@",url); name= person.displayName; [[NSUserDefaults standardUserDefaults] setObject:name forKey:@"userNameLogin"]; [[NSUserDefaults standardUserDefaults] synchronize]; NSLog(@"Name:%@",person.displayName); [self callWebserviceToUploadImage]; }]; } } } -(void)getFacebookProfileInfos { FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc]initWithGraphPath:@"/me?fields=first_name, last_name, picture, email" parameters:nil]; FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init]; [connection addRequest:requestMe completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { if(result) { if ([result objectForKey:@"email"]) { email = [result objectForKey:@"email"]; [[NSUserDefaults standardUserDefaults] setObject:email forKey:@"useremail"]; } if ([result objectForKey:@"first_name"]) { NSLog(@"First Name : %@",[result objectForKey:@"first_name"]); name = [result objectForKey:@"first_name"]; [[NSUserDefaults standardUserDefaults] setObject:name forKey:@"userNameLogin"]; } if ([result objectForKey:@"id"]) { NSLog(@"User id : %@",[result objectForKey:@"id"]); } } [self callfbloginwebservice]; }]; [connection start]; } 
 #import <FBSDKCoreKit/FBSDKAccessToken.h> #import <FBSDKCoreKit/FBSDKGraphRequest.h> 

添加YourViewController.h

 - (IBAction)loginAction:(id)sender { // https://developers.facebook.com/docs/graph-api/reference/user // https://developers.facebook.com/docs/ios/graph if ([FBSDKAccessToken currentAccessToken]) { [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"email,name,first_name,last_name"}] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { if (!error) { NSLog(@"fetched user:%@", result); // Here u can update ur UI like email name TextField } }]; } }