I want to do something on app start if the app is closed unexpectedly last time.(crash, force close), how can I tell it?
I do this by setting a BOOL
to YES
in NSUserDefaults on app startup and when the app enters the foreground.
I set this same value to NO
when the app enters the background.
At the start of didFinishLaunchingWithOptions
(before setting the flag to YES), I check if the flag is currently set to YES
. If it is YES
, then I know the app crashed uncleanly.
However, this does not check if the app was killed in the background (by the OS or the user).
You can handle a force close while in the background (by the OS or the user) by setting a flag when the app enters the background and clearing the flag when the app enters the foreground. Then check for this flag in the didFinishLaunchingWithOptions
method. If this flag is set then you know your app was killed while in the background.
Keep in mind that a force close while in the background should not really be considered an "unexpected close". It's actually quite expected.