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

编程入门 行业动态 更新时间:2024-10-28 19:25:34
本文介绍了在iOS中从Facebook获取用户的个人信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我对C和iPhone开发环境非常新颖。

我正在我的应用程序中实现Facebook登录,以获取用户名,电子邮件和个人资料图片。我已经成功地实现了登录部分,并且已经收到了该人的姓名和用户ID。

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

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

解决方案

要获取用户电子邮件ID,您必须在登录时请求电子邮件的权限。

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

您可以使用GraphPath在新SDK中获取用户电子邮件标识。

[[FBSDKGraphRequest alloc] initWithGraphPath:@me参数:nil] startWithCompletionHandler:^(FBSDKGraphRequestConnection * connection,id result,NSError * error) { if(!error){ NSLog(@获取用户:%@和电子邮件:%@,结果,结果[@电子邮件]); } }]; }

结果会让你所有的用户详细信息和结果[@email]将获得登录用户的电子邮件。

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

NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@graph.facebook/%@/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];

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

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

[[FBSDKGraphRequest alloc] initWithGraphPath:@me 参数:@ {@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]); } }];

I am quite new to objective-C and iPhone Development environment.

I am implementing Facebook login in my app to get User's name, Email and profile Picture. I have successfully implemented login Part and have received name and User ID of the person.

Now i want to get User's Email and Profile Picture from Facebook.But i am not having any Idea how to get it.I am using Facebook IOS SDK v4.0.

How can i fetch User's Profile picture and Email Id from Facebook when i am having User ID?

解决方案

To get user Email ID you must ask permission for email while logging.

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

You can get user email Id in New SDK using GraphPath.

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

result would get you all the user Details and result[@"email"] would get you the email for logged in user.

To get Profile picture you can use

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

or u can also use FBSDKProfilePictureView to get profile Picture by passing user profile Id:

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

Refer to :developers.facebook/docs/facebook-login/ios/v2.3#profile_picture_view

or u can also get both by passing as parameters

[[[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]); } }];

更多推荐

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

本文发布于:2023-10-31 08:08:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545525.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:个人信息   用户   iOS   Facebook

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!