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!

Answers


FBGraphUser doesn't have the property (i'm assuming because its optional/only available if you asked for the email permission) but you should still be able to access it from the dictionary like this:

[user objectForKey:@"email"]

You should check before yo ask the right permissions

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

Need Your Help

How to check if a socket is connected/disconnected in C#?

c# sockets connection

How can you check if a network socket (System.Net.Sockets.Socket) is still connected if the other host doesn't send you a packet when it disconnects (e.g. because it disconnected ungracefully)?

How to find max value in a numpy array column?

python arrays numpy

I can find quite a few permutations of this question, but not this (rather simple) one: how do I find the maximum value of a specific column of a numpy array (in the most pythonic way)?