0

我正在尝试从 Facebook 的 SessionLoginSample 应用程序中获取用户的电子邮件地址。我不确定我在这里做错了什么。我相信这是我获取电子邮件地址的正确区域。关于这部分内容将有助于我返回一封简单的电子邮件,您可以给我任何指导吗?任何帮助将不胜感激!谢谢!

- (void)updateView {
    // get the app delegate, so that we can reference the session property
    SLAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
    if (appDelegate.session.isOpen) {        
        // valid account UI is shown whenever the session is open
        [self.buttonLoginLogout setTitle:@"Log out" forState:UIControlStateNormal];        
        [self.textNoteOrLink setText:[NSString stringWithFormat:@"https://graph.facebook.com/me/friends?access_token=%@",
                                 appDelegate.session.accessTokenData.accessToken]];

//GET THE USER'S EMAIL ADDRESS HERE        
NSString *emailAddress= [user objectForKey:@"email"];         

        self.labelFirstName.text = [NSString stringWithFormat:@"Email: %@", emailAddress];

    } else {        
        // login-needed account UI is shown whenever the session is closed
        [self.buttonLoginLogout setTitle:@"Log in" forState:UIControlStateNormal];        
        [self.textNoteOrLink setText:@"Login to create a link to fetch account data"];        
    }
}
4

1 回答 1

1

请确保您已向用户请求访问其电子邮件地址的权限。这是文档的链接:https ://developers.facebook.com/docs/reference/login/email-permissions/

例子

使用权限创建一个数组

NSArray *permissions = [[NSArray alloc] initWithObjects:@"email",nil];

然后向 openActiveSessionWithReadPermissions: 方法添加权限,如下所示:

[FBSession openActiveSessionWithReadPermissions:permissions
             allowLoginUI:YES
        completionHandler:^(FBSession *session,
                            FBSessionState status,
                            NSError *error) {
            if (error) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                           message:error.localizedDescription
                                           delegate:nil
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];
                 [alert show];

            } 
        }];

此示例使用适用于 iOS 的 Facebook SDK

于 2013-08-29T23:04:13.377 回答