微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

情节提要中的状态恢复

如何解决情节提要中的状态恢复

我试图在我的应用程序中保留和恢复某个VC的状态,尽管实现(我认为)应该可以解决问题,但到目前为止,我仍然无法强制退出该应用程序并恢复状态。我已经阅读了文档和一些示例,但仍在努力使实现生效。我在情节提要中设置了restoreID,并在AppDelegate中具有所有必需的功能

func application(_ application: UIApplication,willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        return true
    }
    
    // For non-scene-based versions of this app on iOS 13.1 and earlier.
    func application(_ application: UIApplication,shouldSaveApplicationState coder: NSCoder) -> Bool {
        coder.encode(1.0,forKey: "version")
        return true
    }

    // For non-scene-based versions of this app on iOS 13.1 and earlier.
    func application(_ application: UIApplication,shouldRestoreApplicationState coder: NSCoder) -> Bool {
        let version = coder.decodeFloat(forKey: "version")
        if(version == 1.0) {
            return true
        }
        return false
    }
    
    @available(iOS 13.2,*)
    // For non-scene-based versions of this app on iOS 13.2 and later.
    func application(_ application: UIApplication,shouldSaveSecureApplicationState coder: NSCoder) -> Bool {
        coder.encode(1.0,forKey: "version")

        return true
    }
    
    @available(iOS 13.2,shouldRestoreSecureApplicationState coder: NSCoder) -> Bool {
        let version = coder.decodeFloat(forKey: "version")
        if(version == 1.0) {
            return true
        }
        return false
    }

在我的视图控制器中:

extension CurrentSessionVC {
   
    override func encodeRestorableState(with coder: NSCoder) {
        super.encodeRestorableState(with: coder)
        
        coder.encode("Hello World",forKey: "message")
    }
    
    override func decodeRestorableState(with coder: NSCoder) {
        super.decodeRestorableState(with: coder)
        
        let message = coder.decodeObject(forKey: "message") as! String
        print(message)
        
    }
}

我设置了一些断点以查看发生了什么,当我执行多任务并清除应用程序时,将触发shouldSaveSecureApplicationState中的断点,然后在我关闭应用程序,停止程序并重新运行它时shouldRestoreSecureApplicationState函数将触发,但是视图控制器未恢复。我已经遍历并测试了其中一个苹果示例(https://developer.apple.com/documentation/uikit/uiviewcontroller/restoring_your_app_s_state),它的工作原理与应有的一样,您清除了该应用程序,然后在下次打开该应用程序时,它从您上次中断的地方开始提取。我是否完全错过了重要的事情?

解决方法

  • 通过在iPhone模拟器上打开主屏幕来挂起该应用程序。您可以向上滑动或使用快捷方式:“ Command(⌘)+ Shift(⇧)+ H”

!!!不要通过在多任务菜单中向上滑动应用程序来手动终止该应用程序。在这种情况下,系统将删除所有状态信息。

  • 通过单击“停止”按钮或使用快捷键“ Command(⌘)+。”来停止Xcode调试器。
  • 使用Xcode重新运行应用

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。