Milan1111
Milan1111

Reputation: 185

Parse receive E-mail FB SDK

I am in trouble with parse user`s email. I can take from user almost everything though fb graph api. But it does not contain email property. Any idea, how to get it? If I do NSlog it always returns null.

{
    NSArray *permissionsArray = @[@"email", @"basic_info"];
    [PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser *user, NSError *error) {
        // Was login successful ?
        if (!user) {
            if (!error) {
                NSLog(@"The user cancelled the Facebook login.");
            }else {
                NSLog(@"An error occurred: %@", error.localizedDescription);
            }
            // Callback - login failed
            if ([delegate respondsToSelector:@selector(commsDidLogin:)]) {
                [delegate commsDidLogin:NO];
            }
        }else if (user.isNew) {
            NSLog(@"User signed up and logged in through Facebook!");
            [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                if (!error) {
                    NSDictionary<FBGraphUser> *prop = (NSDictionary<FBGraphUser> *)result;
                    NSDictionary *userData = (NSDictionary *)result;
                    [[PFUser currentUser] setObject:prop.id forKey:@"fbid"];
                    [[PFUser currentUser] saveInBackground];
                    [[PFUser currentUser] setObject:prop.first_name forKey:@"firstname"];
                    [[PFUser currentUser] saveInBackground];
                    [[PFUser currentUser] setObject:prop.last_name forKey:@"lastname"];
                    [[PFUser currentUser] saveInBackground];
                    NSString *mail = userData[@"email"];
                    [[PFUser currentUser] setObject:mail
                                             forKey:@"email"];
                    [[PFUser currentUser] saveInBackground];


                    NSLog(@"prop %@", prop);



                }else {
                    NSLog(@"User logged in through Facebook!");
                    NSLog(@"Welcome Screen I am %@", [[PFUser currentUser] username]);
                }
            }];
        }
        else {
            //HERE
            NSLog(@"Error getting the FB username %@", [error description]);
        }
        [[PFUser currentUser] saveInBackground];
        // Callback - login successful
        if ([delegate respondsToSelector:@selector(commsDidLogin:)]) {
            [delegate commsDidLogin:YES];
        }
    }];
}

Upvotes: 0

Views: 458

Answers (1)

Timothy Walters
Timothy Walters

Reputation: 16874

Not every Facebook user exposes the same information as part of their public profile, the email field is one in particular where you need to accept that this will often NOT be exposed.

The userData NSDictionary you get back will only be populated with information the person has made public, so handle missing keys as needed per user.

Documentation to read:

Upvotes: 1

Related Questions