StateRestorationSimple
This is a simple implementation of state restoration as outlined in Preserving your app’s UI across launches.
Cross referencing to the sections in that document, I have:
-
“Enable state preservation and restoration for your app”
I have defined
application(_:shouldSaveSecureApplicationState:)
andapplication(_:shouldRestoreSecureApplicationState:)
to both returntrue
. -
“Assign restoration identifiers to your view controllers”
I have set the restoration ID in the storyboard for the view controller.
-
“Encode and decode custom information for your app”
The docs say:
During the preservation process, UIKit calls the
encodeRestorableState(with:)
method of each preserved view and view controller.I have implemented
encodeRestorableState(with:)
(making sure to callsuper
renditions) andapplication(_:willEncodeRestorableStateWith:)
. -
“Create view controllers when asked”
I don’t manually create any view controllers, as the docs say:
UIKit uses your app’s storyboards to recreate your view controllers.
It also says:
UIKit calls each view controller’s
decodeRestorableState(with:)
method to restore its state information.
Note: It is very important to not use UIScene
related objects/methods. The above only works in non-scene based codebases.
In terms of my testing process, I do not force-quit the app. The steps I follow are:
- launch the app from Xcode;
- type some text in text field;
- leave the app (without terminating it) …
application(_:shouldSaveSecureApplicationState:)
is called, butencodeRestorableState(with:)
is not; - only after I am at the home screen do I hit “stop” button in Xcode; and
- relaunch the app from Xcode …
application(_:shouldRestoreSecureApplicationState:)
is called, butdecodeRestorableState(with:)
is not.