从iOS的Facebook获取用户的个人信息
我对Objective-C和iPhone开发环境颇为陌生。从iOS的Facebook获取用户的个人信息
我在我的应用程序中实现Facebook登录以获取用户的姓名,电子邮件和个人资料图片。我已成功实施登录部分并已收到人员的姓名和用户标识。
现在我想从Facebook获取用户的电子邮件和个人资料图片,但我没有任何想法如何得到它。我使用的是Facebook IOS SDK v4.0。
如何从Facebook获取用户的个人资料图片和电子邮件ID时,我有用户ID?
要获取用户电子邮件ID,您必须在登录时询问电子邮件地址的权限。
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"]);
}
}];
}
结果将让你所有的用户详细信息和结果[@“电子邮件”对于登录的用户会得到你的电子邮件。
获得简介图片您可以使用
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];
或u还可以使用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]);
}
}];
嗨dheeraj我试过这个,但我在我的电子邮件中为空。 – 2015-04-07 09:10:52
您在登录时必须要求电子邮件许可。 – 2015-04-07 09:11:37
我已经完成 FBSDKLoginButton * loginButton = [[FBSDKLoginButton alloc] init]; loginButton.center = self.view.center; [self.view addSubview:loginButton]; self.loginButton.readPermissions = @ [@“public_profile”,@“email”]; 在我的viewDidLoad – 2015-04-07 09:13:02
对不起,这是凌乱的答案,这是我的第一个答案。您可以使用FBSDK Graph请求获取用户的所有配置文件信息和FBSDKProfilePictureView类以轻松获取用户的配置文件图片。此代码用于手动Facebook登录UI。
首先,你必须把这段代码登录过程开始:
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];
}
}
}];
而如果登录全成,实现这个方法来获取用户的公众形象;
-(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];
获取用户的个人资料图片当前登录:
FBSDKProfilePictureView *pictureView=[[FBSDKProfilePictureView alloc]init];
[pictureView setProfileID:@"user_id"];
[pictureView setPictureMode:FBSDKProfilePictureModeSquare];
[self.view addSubview:pictureView];
您必须添加清爽码到您的viewDidLoad方法:
[FBSDKProfile enableUpdatesOnAccessTokenChange:YES];
facebookLoginButtonTouched没有被调用 – 2015-04-07 04:58:21
我在我的项目中使用自定义的Facebook登录按钮。所以“facebookLoginButtonTouched”是我的自定义登录按钮的“触及内部”方法。我正在编辑我的答案。 – 2015-04-07 08:55:47
该主题最相关的答案。远离Messy。 谢谢兄弟! – 2015-06-16 08:20:50
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 u r UI like email name TextField
}
}];
}
}
检查这个.... http://stackoverflow.com/questions/29466795/getting-facebook-public-profile-of-a-person-in-ios-app/29466882#29466882 – Shruti 2015-04-06 12:24:01
http:///堆栈溢出。com/questions/22111993/how-to-get-user-info-from-facebook-sdk-in-ios – tharif 2015-04-06 12:24:14
@Shruti没关系我已经尝试过实现这个链接,但我只有名字和姓氏,没有提供获得电子邮件ID – 2015-04-06 12:27:34