首页 > 解决方案 > 在 iOS 中避免/禁用状态保存

问题描述

我正在开发一个具有以下界面(假设)的小型应用程序
启动屏幕->屏幕X->屏幕Y

问题是,当我关闭应用程序并同时再次打开它时,它再次显示最后一次看到的屏幕,然后显示启动屏幕,然后显示初始视图控制器。

我也检查了其他一些应用程序,但它们的行为并非如此,例如谷歌地图。

我还在 AppDelegate 中实现了以下两种方法,如下所示

func application(_ application: UIApplication, shouldSaveApplicationState coder: NSCoder) -> Bool {
        return false
    }

func application(_ application: UIApplication, shouldRestoreApplicationState coder: NSCoder) -> Bool {
        return false
    }

但没有任何工作。有人可以建议我该怎么做

谢谢。

标签: iosobjective-cswift

解决方案


不确定这是否有帮助,但你可以像我一样做并利用NotificationCenter

// In something like viewDidLoad
NotificationCenter.default.addObserver(self, selector: #selector(returnedFromBackground), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)

...

@objc func returnedFromBackground() {
    // Anything you want to preserve/save/do and then segue (or whatever you use) to your launching screen
}

推荐阅读