Learn how to customize the UIKit state restoration process.
Figure 1 illustrates the sequence of calls that happens from the time your app launches until the time it is restored. Restoration occurs during the middle of your app’s initialization, and it proceeds only when a state restoration archive is available and your app delegate’s
application(_: method returns
The first step in the restoration process is to create the view controller objects (either explicitly or implicitly) for your interface. The second step is to decode and restore the state of those objects. Both steps are needed to recreate your view controller hierarchy. For example, after the creation of a navigation controller and its child view controllers, there is no immediate connection between those objects. It is actually the navigation controller’s
decode method that reestablishes the relationships to its child view controllers.
After state restoration finishes, UIKit calls the app delegate’s
application(_: method. Use that method to make any last minute changes or additions to your interface. For example, you might add a login screen to the view controller hierarchy.
Recreate Your View Controllers
During restoration, UIKit tries to create or locate view controller objects from your preserved interface. UIKit first asks you to provide the view controller object. If you do not provide the view controller, UIKit looks for it implicitly. Here is the sequence of steps that UIKit follows to recreate view controllers:
Ask the view controller’s restoration class. A restoration class knows how to create a specific view controller. You designate a restoration class by assigning that class to the
restorationproperty of your view controller. During restoration, UIKit calls the
viewmethod of the restoration class to request a new instance of the view controller, which your method then returns. If you return
Controller(with Restoration Identifier Path: coder:)
nil, UIKit stops trying to create the view controller and leaves it out of the restoration process.
Ask the app delegate. If the view controller had no restoration class, UIKit calls the
application(_:method of the app delegate. If you return
view Controller With Restoration Identifier Path: coder:)
nilfrom that method, UIKit continues searching.
Check for an existing object. UIKit looks for an already created view controller with the exact same restoration path.
Instantiate the view controller from your storyboards. If it still does not have the view controller, UIKit automatically instantiates it from your app’s storyboards.
Before state restoration even begins, UIKit loads your app’s default view controllers from your storyboards. Because UIKit loads these view controllers for free, it is better not to create them using a restoration class or your app delegate. For all other view controllers, assign a restoration class only if the view controller is not defined in your storyboards. You might also assign a restoration class to prevent the creation of your view controller in specific circumstances. For example, you might want to avoid displaying the view controller if the associated restoration archive refers to stale or missing data.
When recreating view controllers in code, always reassign a value to the view controller’s
restoration property in addition to any other initialization. Also assign a value to the
restoration property as appropriate. Assigning these values at creation time ensures that the view controller will be preserved during the next cycle.