报告 iOS 中特定 Web 服务的应用程序崩溃和异常
Reporting app crashes and exceptions to a particular Web service in iOS
我被告知要以自定义方式将该信息发送到 REST 服务,即使是来自应用程序的发布版本...最好的方法是什么?将信息保存到文件中并在应用程序关闭之前发送?您是否应该请求用户报告错误的许可?
提前致谢
我正在使用开源 PLCrashReporter。
步骤是:
下载框架并将其导入您的项目。
在你的 applicationDidFinish...:[=13=]
- (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 限制)。
我被告知要以自定义方式将该信息发送到 REST 服务,即使是来自应用程序的发布版本...最好的方法是什么?将信息保存到文件中并在应用程序关闭之前发送?您是否应该请求用户报告错误的许可?
提前致谢
我正在使用开源 PLCrashReporter。 步骤是:
下载框架并将其导入您的项目。
在你的 applicationDidFinish...:[=13=]
- (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 限制)。