Search code examples
iphoneobjective-cexceptionnsexception

Objective-C/iPhone - NSException capturing as much information as possible


I'm using the following code to capture exceptions in my app:

void uncaughtExceptionHandler(NSException *exception) {
    [FlurryAPI logError:@"Uncaught" message:@"Crash!" exception:exception];
}

Just wondering whether I can pin-point, line numbers, UIView, classes, etc that the error's occurring on. Ideally I'd like as much detailed information as I can get, since it's captured by FlurryAPI analytics.

FlurryAPI: http://www.flurry.com/


Solution

  • I ended up going with this:

    void uncaughtExceptionHandler(NSException *exception) {
        NSArray *backtrace = [exception callStackSymbols];
        NSString *platform = [[UIDevice currentDevice] platform];
        NSString *version = [[UIDevice currentDevice] systemVersion];
        NSString *message = [NSString stringWithFormat:@"Device: %@. OS: %@. Backtrace:\n%@",
                             platform,
                             version,
                             backtrace];
    
        [FlurryAPI logError:@"Uncaught" message:message exception:exception];
    }
    

    UPDATE (based on @TommyG's comment below):

    Add NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler); to the end of your - -(BOOL)application:didFinishLaunchingWithOptions: method in AppDelegate. Then add the above method to the AppDelegate as well.