0
-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user ////////////////////////////// fetch logged in user information
{
    if (FBSession.activeSession.isOpen) {

        [[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(@" f0000000 %@",imageUrl);
                  NSLog(@" email =====   %@",email);
             }
         }];
    }



   NSString *email_id= [user objectForKey:@"email"];
    NSLog(@" fetch yo ----->%@",user.name); /////

     NSLog(@" object    ----->%@",email_id);


}

OutPut------------

f0000000 http://graph.facebook.com/******/picture?type=large
email =====   (null)

f0000000 http://graph.facebook.com/*****/picture?type=large
email =====   (null)

I am getting email null.How can I get email of the user , or current logged user.I am new to ios and have no clue what to do more.

1 Answers1

1

I should really just be explaining instead of posting a bunch of code, but sometimes seeing the code is the best way to learn how everything works together. Here is how I am doing Facebook login in my apps:

// FBSample logic
// The user has initiated a login, so call the openSession method.

FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:@[@"public_profile", @"email", @"user_friends"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
        // Process error
        NSLog(@"ERROR SIGNING IN");
        [self loginFailed];
    } else if (result.isCancelled) {
        // Handle cancellations
        NSLog(@"login cancelled");
        [self loginFailed];
    } else {
        NSLog(@"here 1");
        // If you ask for multiple permissions at once, you
        // should check if specific permissions missing
        if ([result.grantedPermissions containsObject:@"email"]) {
            // Do work
            NSLog(@"here 2");

            NSUserDefaults *fbstatus = [NSUserDefaults standardUserDefaults];
            [fbstatus setValue:@"NO" forKey:@"FBStatus"];
            [fbstatus synchronize];

            [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, first_name, last_name, gender, picture, email, birthday, about, address, age_range, bio, currency, devices, education, favorite_athletes, favorite_teams, hometown, inspirational_people, interested_in, location, political, sports, security_settings, relationship_status, quotes, timezone, website, work, cover, family, photos, videos, friendlists, groups, permissions"}]
             startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
             {

                 if (!error)
                 {
                     NSLog(@"here 3");
                     NSDictionary *userData = (NSDictionary *)result;
                     // userData contains your FBSDKGraphRequest results
                     // do whatever you need to do now that you have Facebook data
                 } else {
                     NSLog(@"ERROR GETTING FB DATA");
                 }

             }];
        }
    }}];

That NSDictionary *userData will contain all of the Facebook information that you are given permissions to. Hope this helps. And don't just copy and paste, read through it so you understand how it works!

MSU_Bulldog
  • 3,501
  • 5
  • 37
  • 73
  • I am sorry but i didnt understand.I didnt get the userdata dictionary – Sandeep Nag Sep 16 '15 at 05:35
  • What did you not understand about the userData dictionary? The userData NSDictionary contains the results from your FBSDKGraphRequest, did you check the console for the NSLog's to make sure that "here 3" is getting called? – MSU_Bulldog Sep 16 '15 at 12:15
  • Yes i checked it in console. and I pasted the code in loggedin-user – Sandeep Nag Sep 17 '15 at 06:06
  • 1
    Have you done all of the other steps for adding Facebook to your app? You have to register it on developer.facebook.com, then add a few things to your info.plist - have you done those steps? – MSU_Bulldog Sep 17 '15 at 12:15
  • 1
    This worked for me, only once I removed the unreasonable amount of parameters. I only used `@{@"fields": @"id, name, first_name, last_name, email, birthday"}` and it finally worked. Thanks for the very readable code snippet. Sorry it took you 2 years to get an upvote, but you deserve one! – jungledev Jun 28 '17 at 21:24