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.
For iOS6 and later there is a way to do this. A side effect of State Restoration is that it will delete the state when there is either a crash during restore or a user manually kills the app. You can use this to your advantage to detect a user manually killing the app.
From the documentation:
The following code assumes that you already have a
BOOL
for crash detection called_didCrashInLastSession
. There are different approaches for getting this value such as this 3rd party library. In your code call the method[self getLaunchType]
to see which type of launch you are dealing with and act on that accordingly. Put the following in your AppDelegate.m:Update: At least one 3rd party SDK breaks this technique: Urban Airship.
You can do it through your device also. Connect your device to your machine. Run xcode and go to organizer. There select your device and device logs. There you can also see crash logs of your app or game.
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"):
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...