I am using Swift 2 and Xcode 7 for iOS 9.2
My, basically finished, game-app creates a complex view (SceneKit
+ SpriteKit
+ random variables + game progress). When I switch to the Menu and back, everything stays the same (just switching views or scenes). However, when the application is closed, it restarts with the main.storyboard's entry-point and my data is gone.
The app should open the same state it was closed on.
I already tried archiving relevant data with NSCoding
to save/load the scene- but it always ends up in a weird or non-informative Error or white screen which do not help me with finding my mistake at all. I do not know at all, where the error is; no useful information is provided from Xcode.
But even if it worked, it would not solve my entry-point problem.
Is there a simple way to just restart from the last state+view that my app had?
Do I need NSCoding
or NSUserDefaults
for this?
You should use State Restoration.
It's designed for that exact purpose to return an app to its previous state, and can be used to restore the state of your app, including its SpriteKit scene.
At some point, the system might need to terminate your app to free up memory for the current foreground app. However, the user should never have to care if an app is already running or was terminated. From the user’s perspective, quitting an app should just seem like a temporary interruption. When the user returns to an app, that app should always return the user to the last point of use, so that the user can continue with whatever task was in progress. This behavior provides a better experience for the user and with the state restoration support built in to UIKit is relatively easy to achieve.
You'll find further details in the App Programming Guide for iOS documentation, under Preserving Your App’s Visual Appearance Across Launches.