不难两步直接上代码
(1)加上监听异常的代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);
return YES;
}
(2)程序崩溃或者发生异常时会走这个处理
void UncaughtExceptionHandler(NSException *exception) {
NSArray *arr = [exception callStackSymbols];
NSString *reason = [exception reason];
NSString *name = [exception name];
//这里是异常报告
NSString * error = [NSString stringWithFormat:@"%@%@%@++++",arr,reason,name];
NSString * docsdir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
NSString *dataFilePath = [docsdir stringByAppendingPathComponent:@"3EClassCrash"];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir = NO;
BOOL existed = [fileManager fileExistsAtPath:dataFilePath isDirectory:&isDir];
if (!(isDir && existed)) {
[fileManager createDirectoryAtPath:dataFilePath withIntermediateDirectories:YES attributes:nil error:nil];
}
//可以根据这个路径去找到保存在本地的异常报告
NSLog(@"%@",dataFilePath);
NSString *path = [dataFilePath stringByAppendingPathComponent:@"3Ecrash.txt"];
NSString * str= [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
BOOL writeSuccess;
if (str.length > 0) {
writeSuccess = [[NSString stringWithFormat:@"%@\n%@",str,error] writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
}else{
writeSuccess = [[NSString stringWithFormat:@"%@",error] writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
if (writeSuccess) {
NSLog(@"写入成功");
}
}
(3)上传的时候可以选择在每次重启进入APP界面的时候上传
网友评论