Restoring Your UI With State Restoration

      1 Comment on Restoring Your UI With State Restoration

In this short article we'll go over how to set the state restoration in your apps and, ultimately, improve the user experience.When your users are using your app, they expect it to behave like it’s constantly running. As you know, iOS can kill your app at any time in order to reclaim system resources. There’s an easy way to preserve the user interface across application starts. In this short article we’ll go over how to set the state restoration in your apps and, ultimately, improve the user experience.

State Restoration

We could certainly argue that not all apps need state restoration. But some apps could certainly use this feature. Ideal candidate would be a form. Imagine your user is filling out a form of some sort. The form might be long, so the user starts a game. iOS system kills your app and when the user comes back to your app, all the form data is gone and the user has to start again. That would be pretty frustrating for the user.

State restoration was intended to solve this problem. The purpose of state restoration is not to save the user data. You have many persistence mechanisms available for that. You should use state restoration to preserve intermediate data. For example: text field/view that the user is editing, scroll offset, currently selected controls, etc…

If you ever used the ‘NSCoding’ protocol, you will feel right at home. ‘UIStateRestoring’ does not extend/implement the ‘NSCoding’ protocol, it’s a protocol of its own. But it behaves in a very similar manner. You have to implement two methods. One for encoding, one for decoding. You set your values for keys and the system will persist the data in an encrypted file on the disk. Let’s see how all this works in code…

The Code

You only need to do three simple things to enable state restoration in our apps.

AppDelegate

Implement the delegate methods in your app delegate:

The method names are self-explanatory. Here you have a chance to decide if you really need/want to restore/save state. A good use case would be to check the version of the app and decide if you want to restore state or not. The business logic will be specific to your app.

Restoration Identifier

Next, you’ll have to provide the restoration identifiers for every view controller on which you want to use state restoration. You don’t have to use state restoration on all of your view controllers. If your view controller doesn’t have the restoration identifier it will simply be skipped when the system tries to save/restore state.

Open your storyboard and set the restoration identifier in the identity inspector:

You can use the same identifier as your storyboard identifier, just like in the screenshot above. You can also set your restoration identifier from code as well.

ViewController

In the demo project we’ll be using a very simple view controller with a couple of elements:

Obviously, your view controllers will be different πŸ™‚ In this simple example we’ll illustrate how you can save/restore states for the segmented control, label, slider and a text field.

To save your state implement the method:

The only function parameter is the ‘coder’. If you ever worked with NSCoding you’ll be familiar with it. If this is your first time encountering NSCoding, it’s a protocol that enables objects to be archived to disk. Check out the Apple documentation for more info. You will use the ‘coder’ to encode values for your custom keys. This will essentially save those values to an encrypted file on the device.

Restoring the state is the opposite of saving it:

We try and decode the value of the text field and if it’s there, we populate the text field and set it as the first responder. If you have multiple text fields, you will have to save some additional data that would indicate which text filed was the last responder when your UI state was being saved.

Testing It

I’ll assume you’re testing on the simulator. To test if your state restoration works you’ll have to follow some steps:

  1. Run the app from Xcode
  2. Play with the UI πŸ™‚
  3. Press the home button on the simulator. At this point the state will be saved.
  4. Stop the app from Xcode
  5. Double-click the home button and kill the app
  6. Now run the app again. Your state should be preserved.

If you don’t follow this specific sequence, your state restoration might not trigger. If you’re testing it on a physical device it should work as expected.

Check it out on this video:

You can clearly see that the app gets killed in the simulator and when we start it again the state of the UI is restored to what it was before the app was put to background.

Conclusion

In this short article you’ve learned how to preserve your UI state and ensure your users will have a smooth experience. It’s very easy to implement state restoration and it’s a very useful feature.

State restoration has its purpose and you shouldn’t abuse it. It’s definitely not meant to be used to save user data. Use it only to save data that’s ‘in progress’. For instance, if your user is filling out a form, or the data is incomplete in some other way because the user didn’t finish an action on your view controller.

The idea behind state restoration is that your app could get killed in the background and when the user starts it again it will seem to her that the app was in memory all this time. Use it wisely πŸ™‚

I hope you learned something new today and that you had fun reading this short article. You can find the example project on GitLab as well as all the code snippets.

As usual… Have a nice day πŸ™‚
~D;

More resources

One thought on “Restoring Your UI With State Restoration

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.