State restoration in storyboard

202 views Asked by At

I am trying to preserve and restore the state of a certain VC in my app, and so far have not been able to force quit the app and restore the state despite implementing (what I think) should do the trick. I've gone through the documentation and a few examples, but am still struggling to get the implementation to work. I set the restorationID in storyboard, and have all the necessary functions in AppDelegate here:

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, *)
    // For non-scene-based versions of this app on iOS 13.2 and later.
    func application(_ application: UIApplication, shouldRestoreSecureApplicationState coder: NSCoder) -> Bool {
        let version = coder.decodeFloat(forKey: "version")
        if(version == 1.0) {
            return true
        }
        return false
    }

and in my view controller:

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

I set some breakpoints to see what is going on, and when I multi task and clear the app, the breakpoint in shouldSaveSecureApplicationState triggers, then when I close the app, stop the program, and re run it the shouldRestoreSecureApplicationState function triggers, but the view controller is NOT restored. I've gone through and tested one of apples examples (https://developer.apple.com/documentation/uikit/uiviewcontroller/restoring_your_app_s_state) and it works exactly how it should, you clear the app and next time you open the app it picks up where you left off. Am I completely missing something important?

1

There are 1 answers

0
AlexanderZ On
  • Suspend the app by opening the Home screen on your iPhone simulator. You can swipe it up or use the shortcut: ‘Command (⌘) + Shift (⇧) + H’

!!! Do not kill the app manually by swiping it up from the multitasking menu. In this case, the system will delete all the state information.

  • Stop the Xcode debugger by clicking on the "STOP" button or use the shortcut: ‘Command (⌘) + .’
  • Rerun the app using Xcode