// I need to

ModernizeUpgrade without breaking it RescueFix what someone else started Build NewTurn an idea into a product Reach More PeopleGet found, get used, include everyone

// Company

Case StudiesExpertiseAbout + CareersResourcesContact Book a call →
≡articleModernize

Restoring iOS App State After an Upgrade

Changed restoration IDs can crash your iOS app on launch after an upgrade. Why state preservation fails this way and a simple fix to avoid it.

Art+Logic Jun 14, 20132 min read

From the Art+Logic archive · written in 2013. The tools may have moved on; the thinking still holds.

Photo by Gunther Wegner, http://www.flickr.com/photos/gwegner/8157955351 Software screen capture

When implementing view state preservation in an iOS app, it’s important to handle the case of restoring state after an app upgrade.  The iOS state preservation APIs automatically encode the restoration IDs of view controllers in order to recreate those controllers after an app restart.

However, if in the normal course of developing a new version of the app those restoration IDs are changed or removed, your users will be greeted with multiple app crashes on startup after upgrading.  The console log will show an error message like:

NSInvalidArgumentException, reason: Storyboard doesn’t contain a view controller with identifier ‘SomeController’

The state restoration APIs were not written to quietly ignore errors, but at least, after a few crashes in a row, iOS will delete the stored state and allow the app a fresh start.

A simple solution to avoid these crashes during app state restoration is to:

  1. Compare your app’s version number to the version number automatically stored by iOS during the last app state preservation
  2. Cancel state restoration if the version numbers don’t match

iOS stores the app version number string under the UIApplicationStateRestorationBundleVersionKey key.

Code for the app delegate’s state restoration methods:

- (BOOL)application:(UIApplication*)application
shouldSaveApplicationState:(NSCoder*)coder
{
   return YES;
}
- (BOOL)application:(UIApplication*)application
shouldRestoreApplicationState:(NSCoder*)coder
{
   BOOL restore = YES;
   // Compare the app version number to the preserved number.  If they differ,
   // cancel state restoration.
   NSString* version =
      [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
   NSString* storedVersion = [coder decodeObjectForKey:
      UIApplicationStateRestorationBundleVersionKey];
   restore = [version isEqualToString:storedVersion];
   return restore;
}

After a routine restart, the app version numbers will match and state restoration will proceed smoothly.  Restarting after an upgrade will cancel state restoration, and start the app from its initial state.

It’s also possible to encode a custom UI version number during state preservation and check it during restoration.  If the UI changes (in ways that could break preserved state) less frequently than new versions of the app, this alternative will allow state restoration to only be canceled when absolutely necessary.

However, it seems safer to rely on the app version number string, which Apple enforces changing with every release, rather than a custom string that developers need to remember to update less frequently.  The code presented here is a foolproof method for preventing at least certain undesirable crashes on startup.

keep going

More to explore in Modernize.

All Modernize resources →
let's talk

Got a problem like this one?

Tell us what's stuck. A real person replies within one business day.

Talk to our team →

Talk to people who know software, not a sales script.

Your first call is with Aja, our VP of Sales. He'll help make sure you're solving the right problem. Here's what you'll cover together:

  1. What you're trying to accomplish

    The business goal first, then the technology.

  2. Possible approaches

    And the tradeoffs of each, in plain language.

  3. Risks you may not have considered

    Technical and business, before they cost you.

  4. Whether AI changes the math

    What's practical, and what's just hype for your project.

  5. Sensible next steps

    Even if the right next step is not us.

book-a-call.ics

Loading the scheduler…

Open the booking page →

Some problems are easier to explain in writing. Send us a few lines about what you're working on, and we'll reply within one business day. Helpful to include:

  1. What the software does, or should do
  2. What's stuck, or what's worrying you
  3. Any deadline or budget you have in mind
note.md

Loading the form…