SwiftUI / iOS 14: How to restore last active document (state?) with document-based (DocumentGroup) app

440 Views Asked by At

I am working with Xcode's provided SwiftUI multi-platform, document-based app template. Everything so far has been "smooth sailing", if you will, however I have no been able to figure out something I though was "built in":

If, for some OS management reason the app gets "killed" (or if intentionally, by stopping it in Xcode), when relaunched, the app "forgets" the document the user was working on, and starts up showing the document browser instead.

I looked on the documentation, and found an article titled "Restoring your app's state with SwiftUI" - this article offers sample code for a non-document-based app, but for the life of me I cannot even begin to understand what it is I am supposed to do to adapt it to a doc-based (DocumentGroup) scenario.

I "understand" the concept of SceneStorage, I was even able to persist some random text declared as a SceneStorage property of the example ContentView, but I cannot understand how am I supposed to use this to, when relaunching the app, have the last open document, well, back open.

I sort of tried to do this at the @main code declaration, but could not make it work. Also, Google has NOT been my friend in this case - have not been able to find a single example of this particular scenario anywhere.

I know I might pass as a "noob" (I am...) and that this is fairly new, so I should expect lack of "stuff" around, and that this should not be so hard, so please forgive my "noob-iness" in advance.

If anyone has any idea about how to achieve this, or can point me to a WORKING sample with a document-based/DocumentGroup app, I will be eternally grateful.

0

There are 0 best solutions below