Facebook SDK 3.0: how to receive user's e-mail?

前端 未结 5 1936
执笔经年
执笔经年 2020-12-04 20:07

In the \"old\" FB iOS SDK I could receive user information via the following:

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys         


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-04 20:19

    if (FBSession.activeSession.isOpen) {
        [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary *user, NSError *error) {
             if (!error) {
                 self.nameLabel.text = user.name;
                 self.emailLabel.text = [user objectForKey:@"email"];
             }
         }];
    }
    

    Because FBGraphUser doesn't have an email @property, we can't access the information like with the name (dot-syntax), however the NSDictionary still has the email kv-pair and we can access it like we would do with a normal NSDictionary.

    Don't forget to ask for the email permission though:

    NSArray *permissions = [[NSArray alloc] initWithObjects:@"email", nil];
    [FBSession sessionOpenWithPermissions:permissions completionHandler:
     ^(FBSession *session, FBSessionState state, NSError *error) {
         [self facebookSessionStateChanged:session state:state error:error];
     }];
    

提交回复
热议问题