Question

I have some odd behaviour in the iOS Simulator with state restoration. I've only recently realized it is only happening in the emulator. I'm wondering if there is something I am doing wrong. But I would also like to share my experience.

I created a single screen project in Xcode 5.0.2. In my application delegate I have only added two methods:

- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
    NSLog(@"I am restore.");
    return YES;
} // */


- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
    NSLog(@"I am save.");
    return YES;
} // */

There are no other changes.

When debugging on an actual iPhone, I get both the log messages; "I am restore" on start up, and "I am save" when I press the home button to stop the app. If I comment out either one or both, I do not get any log messages. It seems that you need both methods for either to work. They don't even get called without the presence of the other.

When debugging in the iOS Simulator, (I don't know how to tell what version of the OS I'm using in the Simulator) I never get either of the log messages. If both methods are present, and only if both are present, I get "Warning: Unable to create restoration in progress marker file" showing up in my log, but I still don't get the log messages I made. So, the mere presence of both will cause a warning, but no force on Earth will allow them to be called. In the simulator. I tried adding a restoration id in the storyboard, but it didn't change anything.

I realize that Apple recommends running on an actual device and they don't guarantee similar behaviour on the simulator. But is there some setting I need to put somewhere to get state restoration working in the simulator? At one point I had it working, and I assumed that my storyboard changes had disrupted it. But with such a minimal app, I'm wondering if there is something more fundamental.

Was it helpful?

Solution

This is not a bug. These methods are called in preparation of state serialization for your app. State serialization never happens in the simulator because the app will not be terminated.

Ultimately, this is one of many differences between the simulator and actual devices. Always test on a device to ensure actual functionality.

Edit: State serialization can happen in the simulator by press the simulated 'home' key. ⌘⇧H

OTHER TIPS

Often, the simulator start acting funny and the solution to most problem there is just to reset it.

In the simulator, click on the 'iOS Simulator menu' > 'Rest Content And Settings...'

I do this I think at least once a day, when the keyboard just stops responding or I get funny messages out of the blue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top