ios9: FBSDK currentAccessToken 为零

ios9: FBSDK currentAccessToken nil

我看了很多相关的帖子,我的解决方案仍然难以辨认。

这是我的 AppDelegate.swift 文件:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

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

  if let accessToken = FBSDKAccessToken.currentAccessToken(){
    print(accessToken)
  }else{
    print("Not logged In.")
  }

  return true
}

   func application(application: UIApplication,
               openURL url: NSURL, options: [String: AnyObject]) -> Bool {


return GIDSignIn.sharedInstance().handleURL(url,
                                            sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String,
                                            annotation: options[UIApplicationOpenURLOptionsAnnotationKey])


}


func application(application: UIApplication, openURL url:NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url,sourceApplication: sourceApplication, annotation: annotation)
 }

所以在应用程序启动时 accessTokennil

当我尝试刷新访问令牌时,它不会这样做,因为您无法引用一个无用户访问令牌。我想它可能会缓存一个过期的用户令牌。这仍然很奇怪,因为我正在调用应该 return 当前用户令牌的 FBSDK,即使它已过期。在我的例子中 returns nil.

以下是我如何尝试使用我的 currentAccessToken

func fetchFBInfoForLogin(){
let parameters = ["fields": "email, first_name, last_name"]

if (FBSDKAccessToken.currentAccessToken() != nil) {
  print("current access token is not nil")
  // User is logged in, do work such as go to next view controller.
  let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath:  "me", parameters: nil)
  graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

    if ((error) != nil)
    {
      // Process error
      print("Error: \(error)")
    }
    else
    {
      print(result.valueForKey("id") as? String)
      print(result.valueForKey("name") as? String)
      print(result.valueForKey("email") as? String)

    }
  })

} else {

  //refresh

  FBSDKAccessToken.refreshCurrentAccessToken{ (connection, result, error) -> Void in
    if error != nil {
      print(error)
      return
    }
    print(FBSDKAccessToken.currentAccessToken())
    print("refresh doesn't work?")
    let parameters = ["fields": "email, first_name, last_name"]
    let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath:  "me", parameters: parameters)
    graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

      if ((error) != nil)
      {
        // Process error
        print("Error: \(error)")
      }
      else
      {
        print(result.valueForKey("id") as? String)
        print(result.valueForKey("name") as? String)
        print(result.valueForKey("email") as? String)

      }
    })

  }   
}
}

供以后参考: 我没有将我的 FBSDK 委托调用放在正确的 AppDelegate 方法中,所以 post:

因此我不得不手动关闭我的 FB 登录,这导致回调的结果为 result.isCancelled,因此我没有获得任何用户令牌。

如果您想要来自登录和注销事件的通知,请实施 FBSDKLoginButtonDelegate 协议。

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, FBSDKLoginButtonDelegate {

    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
        if ((error) != nil) {
            // Process error
        } else if result.isCancelled {
            // Handle cancellations
        } else {
            let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "id, email, name"])
            graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
                if ((error) != nil) {
                    //print("Error: \(error)")
                } else {
                    if let user : NSString = result!.valueForKey("name") as? NSString {
                        print("user: \(user)")
                    }
                    if let id : NSString = result!.valueForKey("id") as? NSString {
                        print("id: \(id)")
                    }
                    if let email : NSString = result!.value(forKey: "email") as? NSString {
                        print("email: \(email)")
                    }
                }
            })
        }
    }
}

HTH.