繁体   English   中英

在 iOS 14 中实现状态恢复

[英]implementing state restoration in iOS 14

我正在尝试在我的应用程序中实现状态恢复,我已经阅读了大量文章和文档,但到目前为止我还没有让它工作。 我已经为所有视图控制器提供了一个恢复 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

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

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

看看这个 不建议远离 SceneDelegate。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM