1
我試圖讓用戶的姓名,電子郵件地址和ID從FBSDKLoginManagerLoginResult,並得到一個錯誤時:FBSDKLoginManagerLoginResult valueForUndefinedKey試圖獲得用戶的電子郵件
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)")
}
}
}
}
而且,我找不到可用密鑰列表
好!除了:valueForKey(而不是value(forKey:和graphRequest.startWithCompletionHandler,而不是graphRequest.start)( – user2976267
請原諒,這是swift ver。3.我將這個添加到我的答案中 – kometen
這就是我討厭swift的原因:他們不喜歡不要使廢棄的方法,他們強迫你重寫整個項目:) – user2976267