【问题标题】:Getting user's Personal info from Facebook in iOS在 iOS 中从 Facebook 获取用户的个人信息
【发布时间】:2015-06-10 20:22:27
【问题描述】:

我对 Objective-C 和 iPhone 开发环境很陌生。

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

现在我想从 Facebook 获取用户的电子邮件和个人资料图片。但我不知道如何获取它。我正在使用 Facebook IOS SDK v4.0。

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

【问题讨论】:

  • @Shruti 没关系,我已经尝试实现此链接,但我只有名字和姓氏,没有提供获取电子邮件 ID 的规定
  • @SimerSarao 我不知道你是如何实现的。我使用了相同的代码,我也得到了电子邮件 ID。你能分享你的代码更清楚吗
  • @Shruti。亲爱的,我正在使用 iOS 版 Facebook 的最新 SDK

标签: ios objective-c facebook facebook-sdk-4.0


【解决方案1】:

要获取用户电子邮件 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"]);
         }
         }];
    }

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]; 

或者您也可以使用 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,我试过这个,但我的电子邮件中显示为空。
  • 登录时您必须征得电子邮件许可。
  • 我已经完成了 FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init]; loginButton.center = self.view.center; [self.view addSubview:loginButton]; self.loginButton.readPermissions = @[@"public_profile", @"email"];在我看来DidLoad
  • 注意:图片URL在[[[result objectForKey:@"picture"] objectForKey:@"data"] objectForKey:@"url"]
  • 获取电子邮件的参数不应为零:请参阅此答案stackoverflow.com/a/31408714/976246
【解决方案2】:

抱歉这个乱七八糟的答案,这是我第一次回答。您可以使用 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 没有被调用
  • 我在我的项目中使用自定义 Facebook 登录按钮。所以“facebookLoginButtonTouched”是我自定义登录按钮的“内部修饰”方法。我现在正在编辑我的答案。
  • 此线程中最相关的答案。远离混乱。谢谢老兄!
  • 尽管您的回答存在一些缺陷,但我努力加分
  • '对 /me 的 GET 请求应包含明确的“字段”参数'
【解决方案3】:
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];

}

【讨论】:

    【解决方案4】:
    #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
                 }
             }];
    
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多