【问题标题】:implementing state restoration in iOS 14在 iOS 14 中实现状态恢复
【发布时间】:2020-09-25 19:22:56
【问题描述】:

我正在尝试在我的应用程序中实现状态恢复,我已经阅读了许多文章和文档,但到目前为止我还不能让它工作。我已经为所有视图控制器提供了一个恢复 ID,并且(我认为)具有使其工作所需的所有必要功能。

AppDelegate

func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        return true
    }
    
    func application(_ application: UIApplication, shouldRestoreSecureApplicationState coder: NSCoder) -> Bool {
        return true
    }
    func application(_ application: UIApplication, shouldSaveSecureApplicationState coder: NSCoder) -> Bool {
        return true
    }
    
    func application(_ application: UIApplication, viewControllerWithRestorationIdentifierPath identifierComponents: [String], coder: NSCoder) -> UIViewController? {
        return coder.decodeObject(forKey: "Restoration ID") as? UIViewController
        
    }
    func application(_ application: UIApplication, didDecodeRestorableStateWith coder: NSCoder) {
        UIApplication.shared.extendStateRestoration()
        DispatchQueue.main.async {
            UIApplication.shared.completeStateRestoration()
        }
    }

在我的视图控制器中:

override func encodeRestorableState(with coder: NSCoder) {
        super.encodeRestorableState(with: coder)
        
        getSetDataFromTable()
        coder.encode(currentSession, forKey: "CurrentSession")
    
    }
    
    override func decodeRestorableState(with coder: NSCoder) {
        super.decodeRestorableState(with: coder)
        
        
        self.currentSession = coder.decodeObject(forKey: "CurrentSession") as! [CurrentSessionElement]
    }
    
    
    override func applicationFinishedRestoringState() {
        
        tableView.reloadData()
    }
    static func viewController(withRestorationIdentifierPath identifierComponents: [String], coder: NSCoder) -> UIViewController? {
            
        guard let restoredSession = coder.decodeObject(forKey: "CurrentSession") as? [CurrentSessionElement] else {
            print("decoding user detail")
            return nil
        }
        
        let vc = CurrentSessionVC()
        vc.currentSession = restoredSession
        return vc
    }

我在所有函数中都设置了断点,当我尝试测试功能时遇到的断点是

加载时: shouldRestoreSecureApplicationState didDecodeRestorableStateWith

清除应用程序时: shouldSaveSecureApplicationState

测试时没有恢复,也没有触发任何视图控制器功能,我错过了什么吗?

【问题讨论】:

    标签: ios swift state-restoration


    【解决方案1】:

    看看this。不建议离开 SceneDelegate。

    【讨论】:

      【解决方案2】:

      我有同样的问题。我删除了 SceneDelegate 并开始正常工作。据我了解,SwiftUI 的恢复以另一种方式工作,与经典 UI 流程不兼容

      【讨论】:

      • 如果您的目标是 iOS 13 或更高版本,不建议删除 SceneDelegate。如 Phantom59 的回答,iOS 13 或更高版本有一种新的状态恢复方法。
      【解决方案3】:

      我昨天才开始了解这个。有两种类型的状态恢复;旧的基于视图控制器的恢复,以及在 iOS 13 及更高版本中引入的新的基于场景的恢复,@Phantom59 答案中的链接很好地解释了它,并且有一些来自 WWDC 2019 的会议。

      您在问题中所做的是旧方式,并且仅在您选择退出基于场景的应用程序生命周期时才有效,不推荐这样做。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-25
        • 2014-11-09
        • 2012-05-23
        • 1970-01-01
        • 2018-02-20
        • 2014-12-15
        • 2012-09-16
        • 1970-01-01
        相关资源
        最近更新 更多