Detect app crashed during load / last time it was run?

Anders Sewerin Johansen picture Anders Sewerin Johansen · Jun 4, 2012 · Viewed 9.1k times · Source

I would like for my app to reset to standard settings if it crashed during startup. Preferably also if it crashed last time it was run.

EDIT: Crittercism has a crittercismDidCrashOnLastLoad method, but it only handles the case of crashing during load. It didn't work properly in the version of the library I used, but this has since been fixed.

Suggestions?

Answer

graver picture graver · Jun 4, 2012

Make 2 functions in your AppDelegate.m file:

void HandleException(NSException *exception) {
    NSLog(@"App crashing with exception: %@", exception);
    //Save somewhere that your app has crashed.
}

void HandleSignal(int signal) {
    NSLog(@"We received a signal: %d", signal);
    //Save somewhere that your app has crashed.
}

Then in your -(BOOL)application:didFinishLaunchingWithOptions before anything else put:

NSSetUncaughtExceptionHandler(&HandleException);

struct sigaction signalAction;
memset(&signalAction, 0, sizeof(signalAction));
signalAction.sa_handler = &HandleSignal;

sigaction(SIGABRT, &signalAction, NULL);
sigaction(SIGILL, &signalAction, NULL);
sigaction(SIGBUS, &signalAction, NULL);