问题
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.
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 :
fatal error: unexpectedly found nil while unwrapping an Optional value
I am using XCode 7.3.1. I am adding the pic of the code:
回答1:
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)
}
}
来源:https://stackoverflow.com/questions/38031044/cannot-print-the-fbsdkaccesstoken-currentaccesstoken-tokenstring-in-my-ios9-ap