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

时间:2015-04-16 12:19:52

标签: ios facebook swift facebook-sdk-4.0

我正在尝试创建个人资料注册页面,并在从“使用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()

            })



    }

}

如何找到解决方法?

2 个答案:

答案 0 :(得分:3)

我遇到了同样的问题

尝试添加这些

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

答案 1 :(得分:2)

您应该从didFinishLaunchingWithOptions

返回此内容

返回FBSDKApplicationDelegate.sharedInstance()。application(application,didFinishLaunchingWithOptions:launchOptions)