我被告知要以自定义方式将该信息发送到 REST 服务,即使是来自应用程序的发布版本……最好的方法应该是什么?将信息保存到文件中并在应用程序关闭之前发送?您是否应该请求用户允许报告错误?
提前致谢
最佳答案
我正在使用开源PLCrashReporter 。 步骤是:
下载框架并将其导入到您的项目中。
在您的应用程序中DidFinish...:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Crash Reports PLCrashReporter *crashReporter = [PLCrashReporter sharedReporter]; if ([crashReporter hasPendingCrashReport]) [self handleCrashReport]; [crashReporter enableCrashReporterAndReturnError:nil]; ... }
在 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) { }]; }
请注意,堆栈跟踪只会在下次应用启动时发送到服务器(由于 iOS 限制)。
关于ios - 报告 iOS 中特定 Web 服务的应用程序崩溃和异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29251795/