Daniel Daniel - 22 days ago 13
iOS Question

App restarts when woken from background

G'day iOS Guru's,

I have searched extensively for an answer, but can't find one (I bet the first response to my question will be to another similar question, but I cant find it).

Anyway, my problem is that I am running a simple map app that the user can drop pins on the map with a customised circle overlay around the dropped pin.

When the app goes into the background (iphone locked or home button pressed), if I re-enter the app within ~ 5 mins, the pins are still there and the app reopens to the last screen.

All good.

However, if I leave the app in the background for longer than 5 mins, the app restarts and all the pins are lost.

I have "Application does not run in background = NO" in the plist, and also enabled "App registers for location updates" under Required background modes.

How can I prevent the app from restarting after it enters the background and load the last opened view?

lxt lxt
Answer

iOS can, and will, terminate your app while it's in the background if it needs additional resources to carry out whatever's going on in the foreground.

You need to ensure that your data is saved/archived when your app is terminated, and unpacked when re-launched so as to go back to where the user last was. The traditional way to do this is to use the applicationDidEnterBackground method, which is called when your app is suspended. You can then save all the data you need in order to resume cleanly in case your app is later terminated.

However, if you're targeting iOS 6 and upwards you can take advantage of a new feature - State Presumption & Restoration (link to documentation). State restoration off-loads some (but not all) of the heavy lifting onto iOS, and it can automatically snapshot your UI and provides easier ways to preserve and restore data.