如何在iOS上使用Facebook SDK 3.1获取用户的电子邮件?

时间:2012-11-20 22:22:28

标签: objective-c ios facebook

我正在使用电子邮件权限打开facebook会话,如下所示:

- (void)facebookOpenSession {
    NSArray *permissions = @[@"email"];
    [FBSession openActiveSessionWithReadPermissions:permissions
                                       allowLoginUI:YES
                                  completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
        [self sessionStateChanged:session state:state error:error];
    }];
}

然后会话状态的重要片段发生了变化:

- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error {
    switch (state) {
        case FBSessionStateOpen: {
            [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
             if (error) {
                //error
             } else {
                 self.myFirstNameLabel.text = user.first_name;
                 self.myLastNameLabel.text = user.last_name;
                 // self.myEmailLabel.text = @"";
             }
         }];
    }
    ...

如何实际提取用户的电子邮件?它是在一个提供的变量中,还是我必须在完成处理程序中进行另一个调用?

提前致谢!

2 个答案:

答案 0 :(得分:26)

FBGraphUser没有属性(我假设因为它是可选的/只有在你要求电子邮件权限时才可用)但你仍然可以从字典中访问它,如下所示:

[user objectForKey:@"email"]

答案 1 :(得分:0)

您应该在提出正确的权限之前进行检查

loginView.readPermissions = @[@"public_profile", @"email"];