Distinguish between an iPhone App crashing and being killed

Gapton picture Gapton · Aug 11, 2011 · Viewed 8.8k times · Source

Hello I am planning to develop a simple iPhone game. I would like to be able to distinguish between a genuine crash, and the user killing the app ( by double tapping home screen and long-clicking it to kill it) .

Can someone please shed some light on what exactly happens when the user kill the app through the multitasking bar.

Answer

sergio picture sergio · Aug 11, 2011

If your app is in the background and suspended when the user kills it, it will receive no notification. This accounts for the majority of cases.

If your app is currently running in the background (there are only very specific categories of apps that can do that), then it receives applicationWillTerminate.

Indeed, Apple is very clear as to the fact that you should save any relevant data before entering the background. Have a look at this (chapter "Responding to Application Termination"):

Even if you develop your application using iOS SDK 4 and later, you must still be prepared for your application to be killed without any notification. The user can kill applications explicitly using the multitasking UI. In addition, if memory becomes constrained, the system might remove applications from memory to make more room. If your application is currently suspended, the system kills your application and removes it from memory without any notice. However, if your application is currently running in the background state (in other words, not suspended), the system calls the applicationWillTerminate: method of your application delegate. Your application cannot request additional background execution time from this method.

EDIT:

about the "saying sorry" thing...

you can certainly do that on the next launch. simply store a key in NSUserDefaults and remove it when the app enters the background (I hope all this sounds familiar to you, otherwise look into the UIApplicationDelegate protocol).

when the app starts up, you check the key; if it is there, then the app was not closed by the user; if the app is not there, then the user at least moved the app to the background and did not experience any sudden termination...