I'm using the open source PLCrashReporter.
The steps are:
Download the framework and import it into your project.
In your applicationDidFinish...:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Crash Reports
PLCrashReporter *crashReporter = [PLCrashReporter sharedReporter];
if ([crashReporter hasPendingCrashReport]) [self handleCrashReport];
[crashReporter enableCrashReporterAndReturnError:nil];
...
}
Add the following methods in your AppDelegate.m:
#pragma mark - Crash Reports
- (void)handleCrashReport
{
PLCrashReporter *crashReporter = [PLCrashReporter sharedReporter];
NSData *crashData;
NSError *error;
crashData = [crashReporter loadPendingCrashReportDataAndReturnError:&error];
PLCrashReport *report = [[PLCrashReport alloc] initWithData:crashData error:nil];
if (!crashData || !report) {
[crashReporter purgePendingCrashReport];
} else {
NSString *stringRepresentation = [PLCrashReportTextFormatter stringValueForCrashReport:report withTextFormat:PLCrashReportTextFormatiOS];
[self sendDataOnLatestCrashToServer:stringRepresentation];
[crashReporter purgePendingCrashReport];
}
}
- (void)sendDataOnLatestCrashToServer:(NSString *)crashString
{
NSDictionary *params = @{
@"StackTrace" : crashString
// Add more parameters as you want
};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:nil];
NSURL *url = [NSURL URLWithString:@"http://www.YOURRESTSERVER.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0f];
[request setHTTPMethod:@"POST"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:jsonData];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
}];
}
Notice that the stack trace will be sent to server only on the next app startup (due to iOS limitation).