Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 931
extreamLearner Avatar asked Aug 07 '15 14:08

extreamLearner


People also ask

How initialize Facebook SDK iOS?

In Xcode, click File > Swift Packages > Add Package Dependency. In the dialog that appears, enter the repository URL: https://github.com/facebook/facebook-ios-sdk. In Version, select Up to Next Major and the default option. Complete the prompts to select the libraries you want to use in your project.

What is Facebook SDK for iOS?

The Facebook SDK is what allows mobile app developers to integrate Facebook within a mobile app. SDK stands for software development kit, and it allows for a website or app to integrate with Facebook seamlessly.

What does SDK mean in Facebook?

The Facebook SDK is a set of software components that developers can include in their mobile app to understand how people use the app, run optimized marketing campaigns and enable Facebook login and social sharing. This course helps you understand the purpose of the Facebook SDK and App Events for Android and iOS.


1 Answers

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

like image 70
Deepakraj Murugesan Avatar answered Sep 24 '22 18:09

Deepakraj Murugesan