How to save and load apps last data+view? (without NSCoding?)

天涯浪子 提交于 2020-01-06 19:52:52

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/35005405/how-to-save-and-load-apps-last-dataview-without-nscoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!