Sandeep Nag
Sandeep Nag

Reputation: 23

cannot fetch email from facebook,ios.Get null always

-(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.

Upvotes: 0

Views: 401

Answers (1)

MSU_Bulldog
MSU_Bulldog

Reputation: 3519

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!

Upvotes: 1

Related Questions