从Facebook SessionLoginSample iOS应用程序获取用户电子邮件

时间:2013-05-27 09:48:00

标签: iphone objective-c xcode facebook-ios-sdk

我正试图从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"];        
    }
}

1 个答案:

答案 0 :(得分: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];

            } 
        }];

此示例使用Facebook SDK for iOS