在iOS中从Facebook获取用户的个人信息 [英] Getting user's Personal info from Facebook in iOS

查看:524
本文介绍了在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:@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 
参数:@ {@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:@"https://graph.facebook.com/%@/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 :https://developers.facebook.com/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获取用户的个人信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆