Skip to content

Instantly share code, notes, and snippets.

@walkerc4
Created August 13, 2015 13:06
Show Gist options
  • Save walkerc4/623c343100af21f25c49 to your computer and use it in GitHub Desktop.
Save walkerc4/623c343100af21f25c49 to your computer and use it in GitHub Desktop.
iOS AppDelegate with multiple crash handlers, including custom one to show local notification that the app has crashed
@interface AppDelegate()
@property (nonatomic, assign) NSUncaughtExceptionHandler *crashHandler1;
@property (nonatomic, assign) NSUncaughtExceptionHandler *crashHandler2;
@end
@implementation AppDelegate
AppDelegate *cSelf;
+ (AppDelegate *)sharedDelegate {
return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[NewRelicAgent startWithApplicationToken:@"######################"];
//get newrelic's crash handler
self.crashHandler1 = NSGetUncaughtExceptionHandler();
[Fabric with:@[CrashlyticsKit, TwitterKit]];
//get crashlytics crash handler
NSUncaughtExceptionHandler *handler = NSGetUncaughtExceptionHandler();
//dont overwrite existing crashhandler
if(self.crashHandler1 && handler != self.crashHandler1) {
self.crashHandler2 = handler;
} else if (handler == self.crashHandler1) {
DebugLog(@"Crash handler is same as 1, not setting 2nd");
} else {
self.crashHandler1 = handler;
}
//needed to call notification from crash handler
cSelf = self;
//setup our own crashHander
NSSetUncaughtExceptionHandler(&customExceptionHander);
return YES;
}
void customExceptionHander(NSException *exception)
{
//notify the user so they can restart their session
UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notification = [[UILocalNotification alloc] init];
[notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:5]];
[notification setAlertBody:@"The App has crashed, you need to restart your it."];
[notification setSoundName:UILocalNotificationDefaultSoundName];
[app scheduleLocalNotification:notification];
//make a copy of exception for dual handling
NSException *ex2 = [exception copy];
//call handler that was already hooked (hopefully crashlytics & newrelic)
if(cSelf.crashHandler2)
cSelf.crashHandler2(ex2);
if(cSelf.crashHandler1)
cSelf.crashHandler1(exception);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment