Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distinguish between an iPhone App crashing and being killed

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.

like image 302
Gapton Avatar asked Aug 11 '11 07:08

Gapton


People also ask

How do you know when my app has been killed?

there's no way to determine when a process is killed. From How to detect if android app is force stopped or uninstalled? When a user or the system force stops your application, the entire process is simply killed. There is no callback made to inform you that this has happened.

Why are apps suddenly crashing on my iPhone?

Random and frequent app crashes in mobile devices usually denote a memory issue like when the device is running low on storage. Other performance issues including symptoms of sluggishness, unresponsiveness and random restarts are also likely to transpire in this case.


3 Answers

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...

like image 135
sergio Avatar answered Oct 24 '22 09:10

sergio


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:

Be aware that the system automatically deletes an app’s preserved state when the user force quits the app. Deleting the preserved state information when the app is killed is a safety precaution. (The system also deletes preserved state if the app crashes at launch time as a similar safety precaution.) If you want to test your app’s ability to restore its state, you should not use the multitasking bar to kill the app during debugging. Instead, use Xcode to kill the app or kill the app programmatically by installing a temporary command or gesture to call exit on demand.

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:

typedef NS_ENUM(NSInteger, LaunchType) {
    LaunchTypeUnknown,
    LaunchTypeNewInstall,
    LaunchTypeNormalLaunch,
    LaunchTypeCrashedLastSession,
    LaunchTypeUserManualQuit,
};

static BOOL hadStateToRestore = NO;
static NSString * const kAppHasEverRunKey = @"appHasEverRun";

- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
    // Called when going into the background
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kAppHasEverRunKey];
    [[NSUserDefaults standardUserDefaults] synchronize];

    return YES;
}

- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
    // Called on start up
    hadStateToRestore = YES;
    return YES;
}

- (LaunchType)getLaunchType
{
    if (_didCrashInLastSession) {
        return LaunchTypeCrashedLastSession;
    }

    if (![[NSUserDefaults standardUserDefaults] boolForKey:kAppHasEverRunKey]) {
        return LaunchTypeNewInstall;
    }

    if (!hadStateToRestore) {
        return LaunchTypeUserManualQuit;
    }

    return LaunchTypeNormalLaunch;
}

Update: At least one 3rd party SDK breaks this technique: Urban Airship.

like image 28
Skotch Avatar answered Oct 24 '22 09:10

Skotch


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.

like image 2
manishnath Avatar answered Oct 24 '22 09:10

manishnath