3

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.

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

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

when clearing the app: shouldSaveSecureApplicationState

Nothing is restoring when tested, and none of the view controller functions are being triggered, am I missing something?

ShedSports
  • 541
  • 2
  • 7
  • 14
  • Take a look at [this](https://developer.apple.com/news/?id=4ixc0yxs). Don’t recommend moving away from SceneDelegate. – Phantom59 Feb 22 '21 at 10:31
  • @Phantom59 could you please help me on this https://stackoverflow.com/questions/76493015/how-to-implement-flutter-state-restoration-in-an-ios-native-app-which-has-a-flut – Shahbaz Hashmi Jun 16 '23 at 19:24

3 Answers3

0

I had the same issue. I removed SceneDelegate and all started to work fine. As I understand, restoration for SwiftUI works in another way, not compatible with classic UI flow

  • If you are targeting iOS 13 or above, deleting SceneDelegate is not the recommended way. There is a new way for state restoration for iOS 13 or above as in Phantom59's answer. – alobaili Mar 12 '22 at 18:14
0

I just started learning about this yesterday. There are two types of state restoration; The old view-controller-based restoration, and the new scene-based restoration introduced in iOS 13 and later, the link in @Phantom59 answer is excellent in explaining it and there are a few sessions from WWDC 2019.

What you are doing in your question is the old way and only works if you opt-out of the scene based app life cycle.

alobaili
  • 761
  • 9
  • 23
  • 1
    Don’t say it’s not recommended. It is perfectly fine to not use SceneDelegate if your app doesn’t support multiple windows – Adam May 13 '22 at 15:25
  • @Adam thanks for the comment. I have modified my answer. – alobaili May 13 '22 at 17:25
0

iOS 13 and later requires scene based state restoration I had issue figuring out from the official demo. Check the following sample app created to demonstrate state restoration.

Official Sample for 13 and later

Scene based state restoration sample