Failed to get user email id when logging in using Facebook SDK in iOS

I get a [null] value when I retrieve the user email id using the code below. In addition, I have already set permission to access email like this.

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

Also retrieving email in the 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 addition, I refer to the FacebookSDK integration docs for a reference to the implementation: https://developers.facebook.com/docs/facebook-login/ios#login-button

+5
source share
1 answer

You can try this code. Based on SDK version 4.0. It is changed in 4.0 compared to the old 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 mode, call this function fetchUserInfo

 [self fetchUserInfo]; 

By calling this method, you will receive an access token after logging in and you want to clearly indicate the permission to the email address. using this method you can receive an email from the user. enjoy

+14
source

All Articles