FBSDKAccessToken currentAccessToken nil after quitting app

天涯浪子 提交于 2019-12-17 09:46:31

问题


I am developing an ios app using the Facebook SDK to login. I have set a LogInViewController as the initial View Controller in the Storyboard, from where the user logins using the FB account.

I have another ViewController which is loaded correctly once the user logs in.

In the AppDelegate file I am checking for currentAccessToken and if it is not nil, I am loading directly the second ViewController, because the user is already logged in.

However, the currentAccessToken is always nil if I quit the app and relaunch it. It only works if I press the home button and re-open the app while it's still running in the background.

Here are the details in the code:

AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    self.customNavigationBar()
    if (!isIcloudAvailable()) {
        self.displayAlertWithTitle("iCloud", message: "iCloud is not available." +
           " Please sign into your iCloud account and restart this app")
        return true
    }

    if (FBSDKAccessToken.currentAccessToken() != nil) {
        self.instantiateViewController("MapViewController", storyboardIdentifier: "Main")
    }

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

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

func applicationWillResignActive(application: UIApplication) {
        FBSDKAppEvents.activateApp()
}

func applicationDidBecomeActive(application: UIApplication) {
        FBSDKAppEvents.activateApp()
}

LogInViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()    
    // Listen to the Facebook notification and when received, execute func handleFBSessionStateChangeWithNotification
NSNotificationCenter.defaultCenter().addObserver(self, selector:"handleFBSessionStateChangeWithNotification:", name: "SessionStateChangeNotification", object: nil)
}

func handleFBSessionStateChangeWithNotification(notification: NSNotification) {
    // Switch to MapViewController when logged in
    if ((FBSDKAccessToken.currentAccessToken()) != nil) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let mapViewController = storyboard.instantiateViewControllerWithIdentifier("MapViewController") as! MapViewController
        self.presentViewController(mapViewController, animated: false, completion: nil)
    }
}

I don't know if it is related, but I am also getting a warning for the MapViewController because there is no segue put towards it from the Storyboard:

Warning: Attempt to present MapViewController whose view is not in the window hierarchy!


回答1:


The problem is because you are calling for FBSDKAccessToken.currentAccessToken() before having called

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

You can check for the access token anytime after calling the above line.

EDIT: Explanation

The above line lets the Facebook SDK process the launchOptions and extract the necessary information which it will require to recognise and persist the user for the application.

In cases where the user is already logged in, this simply initialises the Facebook SDK which in turn logs in the user on the basis of persisted data.




回答2:


I spent a nice half-day banging my head against this issue. In spite of making sure all the delegate methods were present, the FBSDKAccessToken.current() always returned nil.

It turns out that this is because Keychain Sharing is not enabled ( Xcode 8, iOS 10). To fix, go to App -> Capabilities -> Keychain Sharing and turn ON.

Once this is done, you have to still go through the authorization process and return back to the app. Everything should be fine after.




回答3:


If you already tried the upon solutions, but still have the problem, try this.

My co-programmer and I are using the LoginManager().login method provided in Facebook Swift SDK. (Xcode8, Swift 3, iOS 10)

One possible action that caused that issue is when you login successfully and kill the app immediately, then open your app again and the AccessToken.current returns nil. After login, if you wait 20 seconds or longer (we are not sure the exact waiting time, we waited for 7-20 seconds) and kill the app, the problem seemed to be solved.

We are not sure why this is happened, but this solved our problem, and our guess is that there may be a network delay or a bug.




回答4:


For those who still having the problem even after set the FBSDKApplicationDelegate.sharedInstance() on the appdelegate, turn out if u get error on your Xcode console like this one

Falling back to storing access token in NSUserDefaults because of simulator bug

it is simulator bug and i try using real device and it work, the access token is not nil again.. so maybe answer @victor comment about this one on @Kal answer..



来源:https://stackoverflow.com/questions/32950937/fbsdkaccesstoken-currentaccesstoken-nil-after-quitting-app

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