iosuikit-state-preservation

When an app is in background, how to determine if an app is closed by a user or by IOS


I am working on state preservation. I would like to preserver the state of my app if

1.. when it is in background, it is killed by IOS due to the lack of memory

I don't want to preserv the state if

2.. when it is in background, it is killed by user

3.. when it is in foreground, it is killed by user

As mentioned by @matt, 1 and 3 can be easily distinguished: when an app is in foreground, if it is killed by user, the state will not be preserved; The app's state is preserved at the moment it goes into background.

My question is: when an app is already in the background, how to determine if it is killed by IOS or by user.


Solution

  • The decision as you put it is an impossible one. If the app is killed due to lack of memory in the foreground, or if the user summarily kills the app in the background by upward swiping in the app switcher, then state cannot be preserved; this is a crash, and state is removed. If the app is killed due to lack of memory in the background, then the preservation of state happened long ago, i.e. when the app went into the background.

    Thus, if you don't preserve state when the app goes into the background ("is closed by user"), it will never be preserved.