问题描述
我正在 Swift 中为 iOS 9 创建一个应用程序以用于学习目的.当我通过我的应用程序登录 Facebook 时,我试图打印 Facebook 的当前访问令牌.
I was creating an app in Swift for iOS 9 for learning purposes. I was trying to print the current access Token of Facebook at time when I log into Facebook through my app.
当我单击 fblogin 按钮时,它会打开 Safari 并显示我已经授权了我的应用程序,但是当我单击确定"时,它卡在 Safari 页面上并且没有按应有的方式打印令牌.它说:
When I click the fblogin button it opens Safari and shows me that I already have authorized my app but when I click okay it stuck on the Safari page and doesn't print the token as it should. And it says :
致命错误:解包可选值时意外发现 nil
我使用的是 XCode 7.3.1.我正在添加代码的图片:
I am using XCode 7.3.1. I am adding the pic of the code:
推荐答案
可能是此时在完成处理程序中,FBSDKAccessToken.currentAccessToken()
尚未保存,这就是原因它仍然是 nil
.您可以在 FBSDKLoginManagerLoginResult
上使用 @property (copy, nonatomic) FBSDKAccessToken *token;
属性:
It might be that at this point in the completion handler, the FBSDKAccessToken.currentAccessToken()
isn't saved yet, that's why it's still nil
.You can use the @property (copy, nonatomic) FBSDKAccessToken *token;
property on FBSDKLoginManagerLoginResult
:
FBSDKLoginManager().logInWithReadPermissions(["email"], fromViewController: self) { (facebookResult: FBSDKLoginManagerLoginResult!, facebookError: NSError!) in
if let token = facebookResult.token.tokenString {
print(token)
}
}
这篇关于无法在我的 iOS9 应用程序中打印 FBSDKAccessToken.currentAccessToken().tokenString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!