简体   繁体   中英

How to get user's email with Facebook SDK 3.1 on iOS?

I'm opening the facebook session with the email permission like so:

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

And then the important snippet of session state changed looks like:

- (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 = @"";
             }
         }];
    }
    ...

How do I actually extract the user's email? Is it in one of the provided variables, or do I have to make another call within the completion handler?

Thanks in advance!

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

[user objectForKey:@"email"]

您应该在询问正确的权限之前进行检查

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM