退出应用程序后,FBSDKAccessToken currentAccessToken为零 [英] FBSDKAccessToken currentAccessToken nil after quitting app

查看:159
本文介绍了退出应用程序后,FBSDKAccessToken currentAccessToken为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Facebook SDK开发一个ios应用程序来登录。
我已将 LogInViewController 设置为故事板中的初始视图控制器,用户使用FB帐户登录。

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.

我有另一个ViewController在用户登录后正确加载。

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

在AppDelegate文件中我检查 currentAccessToken 如果它不是nil,我直接加载第二个ViewController,因为用户已经登录。

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.

然而,<$如果我退出应用并重新启动它,c $ c> currentAccessToken 总是为零。它只有在我按下主页按钮并在后台仍在后台运行时重新打开应用程序时才有效。

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

我不知道它是否相关,但我也收到了 MapViewController 的警告,因为没有针对它的segue故事板:

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:


警告:尝试呈现其视图不在
窗口层次结构中的MapViewController!

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


推荐答案

问题是因为你要求 FBSDKAccessToken.currentAccessToken()在调用之前

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.

编辑:解释

以上行让Facebook SDK处理launchOptions并提取必要的信息,以便识别并保留用户的应用程序。

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.

如果用户已经登录,这只是初始化Facebook SDK,然后根据持久数据登录用户。

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.

这篇关于退出应用程序后,FBSDKAccessToken currentAccessToken为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆