Cannot print the FBSDKAccessToken.currentAccessToken().tokenString in my iOS9 app

孤街醉人 提交于 2019-12-23 21:15:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!