如何保存和加载应用程序上次数据+查看?(没有 NSCoding?) [英] How to save and load apps last data+view? (without NSCoding?)

查看:45
本文介绍了如何保存和加载应用程序上次数据+查看?(没有 NSCoding?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 iOS 9.2 上使用 Swift 2 和 Xcode 7

I am using Swift 2 and Xcode 7 for iOS 9.2

我的,基本完成的游戏应用程序创建了一个复杂的视图(SceneKit + SpriteKit + 随机变量 + 游戏进度).当我切换到菜单并返回时,一切都保持不变(只是切换视图或场景).但是,当应用程序关闭时,它会以 main.storyboard 的入口点重新启动,而我的数据就消失了.

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.

该应用应打开与关闭时相同的状态.我已经尝试使用 NSCoding 存档相关数据以保存/加载场景 - 但它总是以奇怪或无信息的错误或白屏告终,这根本无法帮助我找到我的错误.我根本不知道错误在哪里;Xcode 没有提供有用的信息.但即使它有效,也无法解决我的入口问题.

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.

有没有一种简单的方法可以从我的应用程序的最后一个状态+视图重新启动?为此我需要 NSCodingNSUserDefaults 吗?

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?

推荐答案

你应该使用 状态恢复.

它专为将应用返回到其先前状态而设计,并可用于恢复应用的状态,包括其 SpriteKit 场景.

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.

在某些时候,系统可能需要终止您的应用以释放当前前台应用的内存.但是,用户永远不必关心应用程序是否已经在运行或已终止.从用户的角度来看,退出应用程序应该只是暂时的中断.当用户返回应用程序时,该应用程序应始终将用户返回到上次使用点,以便用户可以继续执行正在进行的任何任务.这种行为为用户提供了更好的体验,并且 UIKit 内置的状态恢复支持相对容易实现.

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.

您可以在 iOS 应用编程指南 文档,位于在启动时保持应用的视觉外观.

You'll find further details in the App Programming Guide for iOS documentation, under Preserving Your App’s Visual Appearance Across Launches.

这篇关于如何保存和加载应用程序上次数据+查看?(没有 NSCoding?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆