简体   繁体   English

在 iOS 14 中实现状态恢复

[英]implementing state restoration in iOS 14

I am trying to implement state restoration in my app, I've read through numerous articles and documentation but so far I have not been able to get it working.我正在尝试在我的应用程序中实现状态恢复,我已经阅读了大量文章和文档,但到目前为止我还没有让它工作。 I have given all view controller a restoration ID, and (I think) have all the necessary functions to get it to work.我已经为所有视图控制器提供了一个恢复 ID,并且(我认为)拥有让它工作的所有必要功能。

in AppDelegateAppDelegate

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

in my view controller:在我的视图控制器中:

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
    }

I set breakpoints in all the functions and when I try to test the functionality the breakpoints that hit are我在所有函数中设置了断点,当我尝试测试功能时,命中的断点是

when loading: shouldRestoreSecureApplicationState didDecodeRestorableStateWith加载时: shouldRestoreSecureApplicationState didDecodeRestorableStateWith

when clearing the app: shouldSaveSecureApplicationState清除应用程序时: shouldSaveSecureApplicationState

Nothing is restoring when tested, and none of the view controller functions are being triggered, am I missing something?测试时没有任何恢复,并且没有触发任何视图控制器功能,我错过了什么吗?

I had the same issue.我遇到过同样的问题。 I removed SceneDelegate and all started to work fine.我删除了 SceneDelegate 并且一切都开始正常工作。 As I understand, restoration for SwiftUI works in another way, not compatible with classic UI flow据我了解,SwiftUI 的恢复以另一种方式工作,与经典 UI 流程不兼容

Take a look at this .看看这个 Don't recommend moving away from SceneDelegate.不建议远离 SceneDelegate。

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

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