I'm trying to get the users name, facebook id and their email by logging in through my app. The following is in my app delegate:
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI :(FBSessionStateHandler)handler{
NSArray *permissions = [NSArray arrayWithObjects:@"basic_info", @"email", nil];
return [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:allowLoginUI completionHandler:handler];
}
and my completionHandler
code and all is here:
void (^fbCompletionHandler)(FBSession *session, FBSessionState state, NSError *error) = ^(FBSession *session, FBSessionState state, NSError *error) {
switch (state) {
case FBSessionStateOpen:
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[FBSession.activeSession closeAndClearTokenInformation];
break;
default:
break;
}
if (error && [FBErrorUtility shouldNotifyUserForError:error]) {
NSLog(@"%@", error.localizedDescription);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Oops" message:[FBErrorUtility userMessageForError:error] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
if (session.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
[self performLoginOperation:user];
}
}];
}
};
and I'm accessing the users email like so: [user objectForKey:@"email"]
. Most of the time it works, but every now and then I get a nil
value. I'm not sure why since I set it so in the permissions? Is there any other reason for this?
I tried to dig into other SO posts and couldn't find the answer:
Facebook SDK email sometimes returns null - Android
I have similar situation that the user object does not contains the email in response. My case is that the user email address is actually pending for verification. In Facebook graph API document in https://developers.facebook.com/docs/reference/api/user/, for the email field, it stated:
note: this field will not be returned if no valid email address is available for the user
See if this is also the reason for the problems you have encountered.