1

In my application i am doing login with facebook via FacebookSDK.framework

- (IBAction)facebookLoginBtn1:(id)sender {
[FBSession openActiveSessionWithReadPermissions:@[@"email"] allowLoginUI:YES completionHandler:
 ^(FBSession *session, FBSessionState state, NSError *error) {
     AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
     [appDelegate sessionStateChanged:session state:state error:error];
     [[FBRequest requestForMe] startWithCompletionHandler:
      ^(FBRequestConnection *connection,
        NSDictionary<FBGraphUser> *user,
        NSError *error) {
          if (!error) {
              NSString *firstName = user.first_name;
              NSString *lastName = user.last_name;
              NSString *facebookId = user.id;
              NSString *email = [user objectForKey:@"email"];
              NSString *imageUrl = [[NSString alloc] initWithFormat: @"http://graph.facebook.com/%@/picture?type=large", facebookId];
              NSLog(@"%@",email);
              NSLog(@"%@",user.id);
              NSLog(@"%@",imageUrl);
          }
          else
          {

          }
      }];
 }];

}

and code working in my old application , but i am getting only FB id and user name here. I have tried with diffrent types of permissions and facebook id's but can not able to find out what is the problem here .

I have done whole set up perfectly in info plist and facebook developer portle. i am getting only below response as FBGraphUser object

 {
id = FB id;
name = “user name here“;}

as responce . Here no email id i am getting. Can any one help me out here. Its supposed to be minor thing that i am missing.

guru
  • 2,727
  • 3
  • 27
  • 39

2 Answers2

2

Try This code. Based on Facebook SDK version 4.0

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 call this method you get access token after login

[self fetchUserInfo];

pass parameter this way to get email, name etc..

@{@"fields": @"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location ,friends ,hometown , friendlists"}
Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
1

You have to specify which fields you want to get now, the following API call does not return the email even if the user authorized that permission:

/me

This is how you get access to the email again:

/me?fields=id,email

Changelog: https://developers.facebook.com/docs/apps/changelog

andyrandy
  • 72,880
  • 8
  • 113
  • 130