我试图通过我的应用程序登录来获取用户名、facebook id 和他们的电子邮件。以下是我的应用程序委托:
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI :(FBSessionStateHandler)handler{
NSArray *permissions = [NSArray arrayWithObjects:@"basic_info", @"email", nil];
return [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:allowLoginUI completionHandler:handler];
}
我的completionHandler
代码和一切都在这里:
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];
}
}];
}
};
我正在访问用户的电子邮件,如下所示:[user objectForKey:@"email"]
。大多数时候它有效,但时不时地我得到一个nil
价值。我不知道为什么,因为我在权限中设置了它?还有其他原因吗?
我试图深入研究其他 SO 帖子,但找不到答案: