Resolved handling app crash

Discussion in 'iOS Programming' started by straber, Feb 9, 2013.

  1. straber, Feb 9, 2013
    Last edited: Mar 17, 2013

    macrumors member

    Joined:
    Jul 3, 2012
    #1
    I was recently playing with an app and it suddenly crashed (it's happened to all of us). But, when I re-launched the app I got an alert that said, "Our app quit unexpectedly, would you like to send an error report so we can fix this?" This made me curious for two reasons:

    1.) How was their app able to detect that it had crashed?

    2.) What information did their app send that would help them fix the crash?


    Thank you for any help!
     
  2. macrumors 6502a

    Joined:
    Mar 30, 2012
    #2
    It might not be so sinister.
    It could write a key when it exits and detect the
    Value is incorrect at startup.

    2) values of working variables, state, etc. all the usual.
     
  3. macrumors member

    Joined:
    Mar 27, 2012
    #3
    I don't know much about this, but arent all app crashes recorded by the system and written to a log file somewhere? You can see this log file when you plug your device into Xcode.
     
  4. thread starter macrumors member

    Joined:
    Jul 3, 2012
    #4
    It's true, you can get crash logs through Xcode, but that's not what I'm looking for. I'm trying to figure out how an app can detect that it's crashed all on its own after it's been released to customers.
     
  5. macrumors 6502a

    Joined:
    Mar 30, 2012
    #5
    Do this at startup:
    Code:
    	cleanexit = [[NSUserDefaults standardUserDefaults] integerForKey:@"exit"];
        if (cleanexit == 0) {// do something like send crash log}
     	cleanexit = 0;
        [[NSUserDefaults standardUserDefaults] setInteger:launchkeyval forKey:@"exit"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    
    
    and this on exit:

    Code:
     	cleanexit = 1;
        [[NSUserDefaults standardUserDefaults] setInteger:launchkeyval forKey:@"exit"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    
    Now if the value read for cleanexit at startup is zero it is either the program's first launch,
    or it crashed the last time it was run.
     
  6. thread starter macrumors member

    Joined:
    Jul 3, 2012
    #6
    This seems like the best way to do it to me. Thanks! Now my only question is, when my app becomes active, what information does it have access to about the previous crash that I can have it send me?
     
  7. macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #7
    You could keep track of when large tasks are in progress... maybe if it takes 5 seconds to parse a large XML file, for example, you could know which file it was parsing when it crashed.
     
  8. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Rather than rolling your own, you might want to consider incorporating something like TestFlight SDK, which has crash reporting in realtime.
     
  9. thread starter macrumors member

    Joined:
    Jul 3, 2012
    #9
    I ended up writing this function in my app delegate.

    Code:
    void uncaughtExceptionHandler(NSException *exception) {
        NSLog(@"CRASH: %@", exception);
        NSLog(@"Stack Trace: %@", [exception callStackSymbols]);
        // Internal error reporting
    }
    and added this line of code inside appDidFinishLaunching.

    Code:
    NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
    This will call the above function anytime an exception is not handled, and you have access to the exception. From that function you can log the exception however you like, store it to a key in the defaults seems easiest, then check if there is any data for that key when the app launches.
     

Share This Page