How do you implement global iPhone Exception Handling?

Viewed 40582

I have one crash in my iPhone application that does throw an NSException. The crash reports are completely ambiguous in where the error is and what exactly is causing it. Is there a smart way for me to set a top level exception handler somewhere to see what is causing it? I can't replicate the problem myself, but a few of my beta users certainly can.

What's a smart way to handle a problem of this nature?

6 Answers

If you are planning to do it on your own you could use one of these approaches

Approach1:

void onUncaughtException(NSException* exception)
{
//save exception details
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  NSSetUncaughtExceptionHandler(&onUncaughtException);
  //Add coding to find if any exception has occurred from saved details if so send it to server or ask user to comment on the issue.
//Rest of the coding
}

Approach2:

void onUncaughtException(NSException* exception)
{

//Save exception details

}

int main(int argc, char *argv[])
{
    @autoreleasepool {

        NSSetUncaughtExceptionHandler(&onUncaughtException);

        return UIApplicationMain(argc, argv, nil, NSStringFromClass([SGGI_AppDelegate class]));
    }
}



-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
      //Add coding to find if any exception has occurred from saved details if so send it to server or ask user to comment on the issue.
    //Rest of the coding
 }

Approcach3:

int main(int argc, char *argv[])
{
    @autoreleasepool {

        @try {

            return UIApplicationMain(argc, argv, nil, NSStringFromClass([SGGI_AppDelegate class]));
        }
        @catch (NSException *exception) {      
        //Save the exception
        }
        @finally {
        }

    }
}

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
      //Add coding to find if any exception has occurred from saved details if so send it to server or ask user to comment on the issue.
    //Rest of the coding
 }

Note:

  • In my perspective don't try to send the exception details to server at the time of crashing send it when he starts the app again.

  • If you are going to use NSUserDefaults to save exception details then you have to synchronise it at the time of crashing otherwise it won't persist.

The following code snippet does the job.

   - (void)applicationWillTerminate:(UIApplication *)application
   {
       [[NSUserDefaults standardUserDefaults]synchronize];
   }
  • If you prefer to save it on sqlite db then it persists itself no need to call anything to persist at the time of crashing

Another option for tracking crash reports is Plausible CrashReporter, open source code to automatically send you crash reports from the field.

There's also the CrashReporterDemo, another open source option that is a combination of Plausible CrashReporter and some server code to better track crash reports.

And lastly, there's MacDevCrashReporter, a service that appears to have similarities to iOSExceptional.com, suggested in another answer. I have no idea what their terms of service are, as I've not signed up for the beta. Definitely worth checking before getting in too deeply.

Related