简体   繁体   English

iOS-Facebook SDK v.4.5.1-获取用户名和电子邮件

[英]iOS - Facebook SDK v.4.5.1 - fetch user name and email

I am integrating the Facebook login into my app. 我正在将Facebook登录信息集成到我的应用中。 The SDK is perfectly installed - I have tested everything. SDK已完美安装-我已经测试了所有内容。 I am also able to log in and log out perfectly. 我也可以登录并完美注销。 But now the most important part: I don't know how to fetch the data I need (user name, email address, etc.). 但现在最重要的部分是:我不知道如何获取所需的数据(用户名,电子邮件地址等)。

I currently declare the LoginButton in my header file. 我目前在头文件中声明LoginButton。

@property (strong, nonatomic) IBOutlet FBSDKLoginButton *loginButton;

Then I have this in my main file: 然后在我的主文件中有这个:

    loginButton = [[FBSDKLoginButton alloc] init];
loginButton.delegate = self;
loginButton.readPermissions =
@[@"public_profile", @"email", @"user_friends"];
[self.view addSubview:loginButton];

I have also implemented the two methods I need -- and they are correctly called. 我还实现了我需要的两种方法-并且它们被正确调用。

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

    NSLog(@"facebook login button test");



}
- (void) loginButtonDidLogOut:(FBSDKLoginButton *)loginButton{
    NSLog(@"facebook logout button test");
}

What is the next step? 你下一步怎么做? All the tutorials online are outdated or in Swift. 所有在线教程都已过时或在Swift中。 I mainly need to know the user's name, email and if he is logged in or not (for later on). 我主要需要知道用户的姓名,电子邮件以及他是否登录(稍后再说)。

Thanks a lot. 非常感谢。

You can get it from following method. 您可以从以下方法获取它。

- (void)getFacebookData{
    if ([FBSDKAccessToken currentAccessToken]) {
        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"first_name, last_name, picture.type(large), email, name, id, gender"}]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error) {
                 NSLog(@"fetched user:%@", result);
             }
         }];
    }
}

Then call this method as follow. 然后按以下方式调用此方法。

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

    NSLog(@"facebook login button test");

   [self getFacebookData];

}

By this way you can get all information about user. 通过这种方式,您可以获得有关用户的所有信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM