iOS 6 Saving/Restoring app state feature

2019-03-15 09:58发布

问题:

I'm trying to use the new Saving/Restoring app state feature on iOS 6, but (application:shouldRestoreApplicationState:) method is not called if I killed the app so it doesn't restore the app state, but If I'm running it from the debugger it gets called.

It worked when I added (Application does not run in background) in the plist and set it to YES, I don't want to set it to YES though. I was wondering if anyone got it working without setting the (Application does not run in background) to YES.

回答1:

From the Apple docs:

The system automatically deletes an app’s preserved state when the user force quits the app. Deleting the preserved state information when the app is killed is a safety precaution. (As a safety precaution, the system also deletes preserved state if the app crashes twice during launch.) If you want to test your app’s ability to restore its state, you should not use the multitasking bar to kill the app during debugging. Instead, use Xcode to kill the app or kill the app programmatically by installing a temporary command or gesture to call exit on demand.

From experience, the easiest way is to put your app into the background by pressing the home button (or Command+Shift+H for the simulator). Then use the Xcode stop button. As the docs suggests, a debug exit gesture also works.

Update:

I found a small work around. The app saves its state in Library/Saved Application State/[Bundle ID]-[App Name].savedState/data.data.

When using the simulator, you can copy this file and drop it in anytime you want to restore to that specific state.

Similarly, on a device you can generate a .xcappdata archive from the organizer. Then, you can upload the .xcappdata archive when you want to restore from that saved state.



标签: ios ios6