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

问题描述:

使用以下代码获取用户的电子邮件ID时,我得到[null]值. 另外,我已经设置了访问电子邮件的权限.

I am getting [null] value when I am fetching the user's email-id using below code. Also, I already set the permission to access the email like this.

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

还获取委托中的电子邮件详细信息.

Also Fetching the email details in 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"]);


             }
         }];
    }

}

请为此提供任何解决方案.

Please provide any solution in this.

此外,我还参考了FacebookSDK集成文档以获取实现链接: https ://developers.facebook.com/docs/facebook-login/ios#login-button

Also, I refer the FacebookSDK integration docs for the implementation link:https://developers.facebook.com/docs/facebook-login/ios#login-button

您可以尝试以下代码.基于Facebook SDK版本4.0.与旧版本相比,它在4.0中进行了修改.

You can try this code. Based on Facebook SDK version 4.0. It is modified in 4.0 when compared with the older version.

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

In viewdidload method call this function fetchUserInfo

[self fetchUserInfo];

通过调用此方法,您将在登录后获得访问令牌,并且您想清楚地指定有关电子邮件的权限.通过这种方法,您可以获取用户的电子邮件.享受

By calling this method you will get the access token after login and you want to clearly specify in permission about the email. by this method you can able to fetch the email of the user. enjoy