我正在构建一个活动跟踪应用程序。应用程序功能正在跟踪您为某项活动所花费的时间。用户按下 START 按钮后,我初始化一个 NSTimer,在 CoreData 中创建一个对象并更改一些按钮、标签的状态......
问题是如何在用户按下主页按钮后恢复应用状态。我可以恢复应用程序状态,以防用户从后台模式重新打开它而不会被终止。但如果它在后台终止,我无法恢复。
你能建议我解决这个问题吗?我是否可以保持应用程序不被终止或更好的解决方案?
预先感谢!
我正在构建一个活动跟踪应用程序。应用程序功能正在跟踪您为某项活动所花费的时间。用户按下 START 按钮后,我初始化一个 NSTimer,在 CoreData 中创建一个对象并更改一些按钮、标签的状态......
问题是如何在用户按下主页按钮后恢复应用状态。我可以恢复应用程序状态,以防用户从后台模式重新打开它而不会被终止。但如果它在后台终止,我无法恢复。
你能建议我解决这个问题吗?我是否可以保持应用程序不被终止或更好的解决方案?
预先感谢!
Doing this really well is a fairly long topic, but the concept is called State Restoration. In its basic form (which is all you probably need), it's actually pretty simple and the system will do most of the work for you automatically if you ask for it. Some pointers:
NSTimer
to "count" time. That approach doesn't work. You can use an NSTimer
to update the UI, but you should determine how much time has passed by subtracting the start time from the current time. Given that, you don't need to run in the background to track time passage.encodeRestorableStateWithCoder:
and decodeRestorableStateWithCoder:
in your view controllers to keep track of what object you were working with. All of this is explained in the State Preservation and Restoration documentation linked above.