swift - 尝试获取用户电子邮件时的 FBSDKLoginManagerLoginResult valueForUndefinedKey

标签 swift facebook-ios-sdk

我正在尝试从 FBSDKLoginManagerLoginResult 获取用户名、电子邮件和 ID,但出现错误:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key email.'

代码如下:

   class ViewController: UIViewController, FBSDKLoginButtonDelegate {

        @IBOutlet weak var fbLogin: FBSDKLoginButton!
        override func viewDidLoad() {
            super.viewDidLoad()
       fbLogin.readPermissions = ["public_profile", "email"]
            fbLogin.delegate = self
    }

        func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {


            if ((error) != nil)
            {
                NSLog("User Logged In")
            }
            else if result.isCancelled {
                 NSLog("User cancels")
            }
            else {
                if result.grantedPermissions.contains("email")
                {             
                   let userEmail : String = result.valueForKey("email") as! String //here I get crash
                    NSLog("User Email is: \(userEmail)")
                }
            }
        }
    }

此外,我找不到可用键的列表

最佳答案

看起来您需要创建一个 FBSDKGraphRequest。我有

func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: NSError!) {

    if ((error) != nil) {
        // Process error
    } else if result.isCancelled {
        // Handle cancellations
    } else {
        let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "id, email, name"])
        graphRequest.start(completionHandler: { (connection, result, error) -> Void in
            if ((error) != nil) {
                print("Error: \(error)")
            } else {
                if let user : NSString = result!.value(forKey: "name") as? NSString {
                    //print("user2: \(user)")
                    self.userLabel.text = "Logged in as \(user)"
                }
                if let id : NSString = result!.value(forKey: "id") as? NSString {
                    self.fbId = id
                    //print("id: \(id)")
                }
                if let email : NSString = result!.value(forKey: "email") as? NSString {
                    print("email: \(email)")
                }
            }
        })
    }
}

这个例子使用的是 swift 版本。 3(xcode 测试版 3)。

关于swift - 尝试获取用户电子邮件时的 FBSDKLoginManagerLoginResult valueForUndefinedKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38660979/

相关文章:

swift - 使用 UITextChecker 对 Swift 5 中的希伯来语文本进行拼写检查

swift - 如果未在 init 中添加,则场景子项不会显示

swift - 当新的子/节点/监听器添加到 Firebase 连接时,如何获取 Firebase 消息?

ios - Podfile:本地 pod 相对于 Projectpath 的路径可能吗?

ios - 无法在 iOS 上使用 Facebook 登录(SDK 无法构建授权 URL)

ios - 在 Facebook iOS SDK 中找不到 FBDialogClosePNG.h

facebook-graph-api - 使用 Swift 在 iOS SDK 中处理 Facebook Graph API 结果

swift - Swift 3.1 中#function 文字的奇怪值

ios - iPhone 中的 fbconnect API 响应

ios - 如何在 Swift 中以编程方式更改 Facebook 按钮的文本?