iOS Facebook SDK 4.0.1获取用户的电子邮件

时间:2015-04-12 14:04:02

标签: ios facebook swift facebook-ios-sdk

我正在尝试使用以下内容获取用户的电子邮件地址:

fbLoginManager.logInWithReadPermissions(["email", "public_profile"], handler: { (loginResult, error) -> Void in

                if error != nil {
                    //handle error
                } else if loginResult.isCancelled {
                    //handle cancellation
                } else {
                    //Logged in
                    self.loggedinUser.fbToken = FBSDKAccessToken.currentAccessToken().tokenString

                    var graphReq = FBSDKGraphRequest(graphPath: "me", parameters: nil).startWithCompletionHandler { (connection, result, error) -> Void in
                        if let user = result as? NSDictionary {
                            var email = result.objectForKey("email") as? String
                            var name = result.objectForKey("name") as? String
                            self.loggedinUser.email = email ?? nil
                            self.loggedinUser.fullName = name ?? nil

                        }
                    }
                }
            })

但是在loginResult对象中,不会发送用户的电子邮件。知道如何解决这个问题吗?

感谢

1 个答案:

答案 0 :(得分:1)

您必须在parameters中提供您想要的内容:

FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "email"]).startWithCompletionHandler({ (connection, result, error) -> Void in

例如,要获得多个字段:

parameters: ["fields": "id, name, email, picture.type(large)"]