Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 863
user1601540 Avatar asked Nov 20 '12 22:11

user1601540


People also ask

What is the latest version of Facebook SDK for iOS?

The current version of the Facebook SDK for iOS is version 14.1. 0. Code and samples for the Facebook SDK for iOS are available on GitHub. When you use the Facebook SDK for iOS, follow the Facebook Open Source Terms of Use and Privacy Policy.

What is Facebook SDK iOS?

The Facebook SDK enables: Facebook Login - Authenticate people with their Facebook credentials. Share and Send dialogs - Enable sharing content from your app to Facebook. App Events - Log events in your application.

How big is the Facebook SDK?

What does this mean? By splitting up the Facebook Android SDK, you'll be able to integrate only the features you need. This makes Android apps simpler to develop and reduces app size. Today, the whole SDK is 335K.


1 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"]
like image 197
Edwin Iskandar Avatar answered Oct 13 '22 20:10

Edwin Iskandar