Woah...just looking into this myself. They briefly gloss over using NSUserActivity for state restoration in the WWDC video about supporting multiple windows on iPad....
From what I can tell, this throws the entire working fairly easy to use state restoration machinary out the window? Or am I missing something? Returning a NSUserActivity from a sceneDelegate's method -stateRestorationActivityForScene: seems to prevent the AppDelegate from receving -application:shouldSaveApplicationState:
So, developers are left to reimplement the entire state restoration machinary? What a mess?
In the sample I see relating to this, there's a SceneDelegate method that looks like this:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let userActivity = connectionOptions.userActivities.first ?? session.stateRestorationActivity {
if !configure(window: window, with: userActivity) {
print("Failed to restore from \(userActivity)")
}
}
// If there were no user activities, we don't have to do anything.
// The `window` property will automatically be loaded with the storyboard's initial view controller.
}
So I see there's UIConnectionOptions that has a property:
@property (nonatomic, readonly, copy) NSSet *userActivities;
Which is documented:
@property (nonatomic, readonly, copy) NSSet<NSUserActivity *> *userActivities;
If this property contains one or more NSUserActivity objects, use those objects to restore the state of your scene to its previous configuration. You create user activity objects at key moments in your scene's lifetime and register them with the system. Your activity objects specify the tasks your scene was managing. For example, if the user was browsing a web page, your activity object contains the URL of that page. Use the information in the activity object to configure your scene's UI and restore any data associated with the given task.
This property does not contain user activity objects related to Handoff. At connection time, UIKit delivers only the type of a Handoff interaction in the handoffUserActivityType property. Later, it calls additional methods of the delegate to deliver the
NSUserActivity
object itself.
---
Then there's also a stateRestorationActivity property on UISceneSession. Is the scene session's activity in the connection options set? What's the strategy here? Seems confusing and wasn't explained too well. The multiple window sample just stores a string which maps to an image name and loads it in a view controller that displays a UIimageView and pushes it on a navigation stack. Pretty simple, but many apps have deep navigation stacks, tab bars, ect., and the old state restoration API seemed a million times superior?
Not sure why they didn't just add a UIWindowController class for multiple windows and just have window controllers conforms to UIStateRestoring....