0

I've been told to send that information in a custom way to a REST service even from release versions from an app... what should the best way to do this be? Saving information into a file and sending it before the app closes? Should you request the user's permission for reporting errors?

Thanks in advance

AppsDev
  • 12,319
  • 23
  • 93
  • 186
  • Check these questions, these may help you: http://stackoverflow.com/questions/22654089/how-to-get-iphone-app-crash-log-file-from-iphone-programmatically http://stackoverflow.com/questions/4737701/get-previous-run-crash-logs-on-iphone?rq=1 – atulkhatri Mar 25 '15 at 09:36

1 Answers1

1

I'm using the open source PLCrashReporter. The steps are:

  1. Download the framework and import it into your project.

  2. In your applicationDidFinish...:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // Crash Reports
        PLCrashReporter *crashReporter = [PLCrashReporter sharedReporter];
        if ([crashReporter hasPendingCrashReport]) [self handleCrashReport];
        [crashReporter enableCrashReporterAndReturnError:nil];
        ...
    }
    
  3. 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).

Asaf
  • 2,158
  • 2
  • 25
  • 40