iOS中使用Facebook SDK登录时无法获取用户的电子邮件地址

5

当我使用以下代码获取用户的电子邮件地址时,我得到了[null]值。 此外,我已经设置了访问电子邮件的权限,如下所示。

{
self.fbLoginButton.readPermissions = @[@"public_profile", @"email", @"user_friends"];
}

还需要在delegate中获取电子邮件的详细信息。
- (void)loginButton:(FBSDKLoginButton *)loginButton
didCompleteWithResult:  (FBSDKLoginManagerLoginResult *)result
error:  (NSError *)error
{
    if ([FBSDKAccessToken currentAccessToken])
    {
        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error) {
                 NSLog(@"fetched user:%@", result);
                 NSLog(@"fetched user:%@ and Email : %@", result,result[@"email"]);


             }
         }];
    }

}

请在此提供任何解决方案。
同时,我参考了FacebookSDK集成文档的实现链接:https://developers.facebook.com/docs/facebook-login/ios#login-button
1个回答

14

你可以尝试这段代码。基于Facebook SDK 4.0版本。相较于旧版本,它在4.0版本中进行了修改。

App delegate.m文件中:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                          openURL:url
                                                sourceApplication:sourceApplication
                                                       annotation:annotation];
}

viewController.m文件中

- (IBAction)btnFacebookPressed:(id)sender {
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
     {
         if (error)
         {
             // Process error
         }
         else if (result.isCancelled)
         {
             // Handle cancellations
         }
         else
         {
             if ([result.grantedPermissions containsObject:@"email"])
             {
                 NSLog(@"result is:%@",result);
                 [self fetchUserInfo];
             }
         }
     }];
}

-(void)fetchUserInfo
{
    if ([FBSDKAccessToken currentAccessToken])
    {
        NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);

        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location ,friends ,hometown , friendlists"}]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error)
             {
                 NSLog(@"resultis:%@",result);
             }
             else
             {
                 NSLog(@"Error %@",error);
             }
         }];

    }

}

在viewdidload方法中调用此函数fetchUserInfo。

[self fetchUserInfo];

通过调用此方法,您将在登录后获得访问令牌,并且您希望在权限中明确指定电子邮件。通过此方法,您可以获取用户的电子邮件。享受吧。


感谢您提供的解决方案。 - extreamLearner
我意识到这是一个选项,但它太糟糕了,为什么要有完成处理程序,如果它实际上从来没有被调用呢?哈哈 - ChuckKelly

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接