0

FBSDKAccessToken currentAccessToken nil after quitting app

I looked at many related threads and the solution in my case is still indiscernible.

Here is my AppDelegate.swift file:

    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)
 }

So right on app launch the accessToken is nil.

When I try to refresh the access token, it won't do it because you can't referesh a nil user access Token. I imagine that maybe it caches an expired user Token. It is still strange because I am calling the FBSDK which should return the current User Token, even if it is expired. IN my case it returns nil.

Here is how I try to use the my 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)

      }
    })

  }   
}
}

For future reference: I was not putting my FBSDK delegate call in the right AppDelegate method à la this SO post: Facebook SDK login never calls back my application on iOS 9

Hence why I had to manually close my FB login, which caused the result from the callback to be result.isCancelled and thus why I wasn't getting any user tokens.

Thalatta
  • 4,510
  • 10
  • 48
  • 79

1 Answers1

1

If you want notifications from login- and logout-events implement the FBSDKLoginButtonDelegate protocol.

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.

kometen
  • 6,536
  • 6
  • 41
  • 51
  • I wasn't handling the `result.isCancelled` case, which is what is being returned by the FBSDK delegate method handler. What does that mean? – Thalatta Aug 28 '16 at 16:29
  • 1
    I'm not sure to be honest. :-) I can't remember where the code was from, probably SO modified to swift 2. And is usually either error or OK, most often OK. – kometen Aug 28 '16 at 16:43