【问题标题】:Getting facebook public profile of a person in ios app在 ios 应用程序中获取某人的 facebook 公开个人资料
【发布时间】:2015-06-10 14:04:22
【问题描述】:

我正在开发一个需要 Facebook 登录的 ios 应用程序。我已经成功实现了登录过程。但是现在我无法找到如何以及在何处获取用户的个人资料信息,如名字、姓氏、个人资料图片等...我的应用有权访问名字、姓氏、个人资料图片和电子邮件。

【问题讨论】:

标签: ios objective-c facebook-login


【解决方案1】:

以下是使用最新 Facebook SDK v4.0.1 获取人员 public_profile 的代码。您需要使用 FBSDKProfile 来获取个人资料。

-(void)viewDidLoad{
        FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
        [self.view addSubview:loginButton];

        self.loginButton.readPermissions = @[@"public_profile", @"email", @"user_friends"];
        self.loginButton.delegate = self;
        [FBSDKProfile enableUpdatesOnAccessTokenChange:YES];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(profileUpdated:) name:FBSDKProfileDidChangeNotification object:nil];

         }

    -(void)profileUpdated:(NSNotification *) notification{
         NSLog(@"User name: %@",[FBSDKProfile currentProfile].name);
         NSLog(@"User ID: %@",[FBSDKProfile currentProfile].userID);
    }

    - (void)  loginButton:(FBSDKLoginButton *)loginButton
    didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result
                    error:(NSError *)error{

    }

    - (void)loginButtonDidLogOut:(FBSDKLoginButton *)loginButton{

    }

【讨论】:

  • 这个函数根本没有被调用 - (void) loginButton:(FBSDKLoginButton *)loginButton didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result error:(NSError *)error
  • 请确保您已将 包含在 @interface .h 文件中
  • 是的,我已经包含了它,但是该函数没有被调用。
  • 您的新编辑让我提取用户 ID 和姓名谢谢。你能告诉我如何提取电子邮件和个人资料图片
  • FBSDKProfilePictureView 类提供了一种将某人的 Facebook 个人资料图像添加到您的视图的简单方法。只需在实例上设置 profileID 属性。您可以将其设置为我的值以自动跟踪当前登录的人。
【解决方案2】:

使用 Notification 方法,当它从 facebook 接收信息时,它可以调用 facebook 委托方法

 [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleFBSessionStateChangeWithNotification:)
                                                 name:@"SessionStateChangeNotification"
                                               object:nil];

然后实现这个方法

-(void)handleFBSessionStateChangeWithNotification:(NSNotification *)notification{
 NSDictionary *userInfo = [notification userInfo];

FBSessionState sessionState = [[userInfo objectForKey:@"state"] integerValue];
NSError *error = [userInfo objectForKey:@"error"];
// Usually, the only interesting states are the opened session, the closed session and the failed login.
if (!error) {
    // In case that there's not any error, then check if the session opened or closed.
    if (sessionState == FBSessionStateOpen) {

        [FBRequestConnection startWithGraphPath:@"me"
                                     parameters:@{@"fields": @"first_name, last_name, picture.type(normal), email"}
                                     HTTPMethod:@"GET"
                              completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                  if (!error) {
                                      // Set the use full name.
                                      NSLog(@"first name and last name is %@",[NSString stringWithFormat:@"%@ %@",
                                                                               [result objectForKey:@"first_name"],
                                                                               [result objectForKey:@"last_name"]
                                                                               ]);
                                      NSString *firstName = [NSString stringWithFormat:@"%@",[result objectForKey:@"first_name"]];
                                      NSString *middleName = [NSString stringWithFormat:@"%@",[result objectForKey:@"middle_name"]];
                                      NSString *lastName = [NSString stringWithFormat:@"%@",[result objectForKey:@"last_name"]];
                                      NSString *email = [NSString stringWithFormat:@"%@",[result objectForKey:@"email"]];
                                      // Set the e-mail address.
                                      NSLog(@"email is %@", [result objectForKey:@"email"]);


                                  }
                                  else{


                                      NSLog(@"%@", [error localizedDescription]);
                                  }
                              }];


        // [self.btnToggleLoginState setTitle:@"Logout" forState:UIControlStateNormal];

    }
    else if (sessionState == FBSessionStateClosed || sessionState == FBSessionStateClosedLoginFailed){
        // A session was closed or the login was failed. Update the UI accordingly.
        //  [self.btnToggleLoginState setTitle:@"Login" forState:UIControlStateNormal];
        // self.lblStatus.text = @"You are not logged in.";



    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-03
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多