Unable to get the email id of the User when Log-in with Facebook SDK in iOS

丶灬走出姿态 提交于 2020-01-03 15:31:23

问题


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.

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


回答1:


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

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

In viewController.m file

- (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);
             }
         }];

    }

}

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



来源:https://stackoverflow.com/questions/31880464/unable-to-get-the-email-id-of-the-user-when-log-in-with-facebook-sdk-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!