FBSDKAccessToken.currentAccessToken().userID 有时返回 Nil

FBSDKAccessToken.currentAccessToken().userID sometimes returning Nil

我正在尝试创建个人资料注册页面,并在从 'Login with Facebook' 页面转出后从 Facebook 分配图像。然而在这条线上...

        let userID = FBSDKAccessToken.currentAccessToken().userID

...有时它运行良好但有时返回 'nil',我得到

  ' 2015-04-16 15:08:41.195 FacebookLogin[65906:4225996] [Error]: invalid session token (Code: 209, Version: 1.7.1) 
 fatal error: unexpectedly found nil while unwrapping an Optional value
 (lldb) '

完整代码如下:

 override func viewDidLoad() {
    super.viewDidLoad()



    if let currentUser = PFUser.currentUser() {

        let accessToken = FBSDKAccessToken.currentAccessToken()

        let userID = FBSDKAccessToken.currentAccessToken().userID

        println(userID)
        let url = NSURL(string: "https://graph.facebook.com/"+userID+"/picture?width=300&height=300")

        let urlRequest = NSURLRequest(URL: url!)


        NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue(), completionHandler: {
            response, data, error in



            let image = UIImage(data: data)

            self.profilePicture.image = image


            currentUser.saveInBackground()

            })



    }

}

如何找到解决方法?

我遇到了同样的问题

尝试添加这些

self.fbLoginButton.delegate = self
FBSDKProfile.enableUpdatesOnAccessTokenChange(true)

你应该return 这个来自 didFinishLaunchingWithOptions

return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)