美文网首页
iOS在APP中添加崩溃检测并将崩溃信息存储到本地,后期上传

iOS在APP中添加崩溃检测并将崩溃信息存储到本地,后期上传

作者: _菩提本无树_ | 来源:发表于2018-05-24 11:56 被阅读0次

不难两步直接上代码

(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界面的时候上传

相关文章

网友评论

      本文标题:iOS在APP中添加崩溃检测并将崩溃信息存储到本地,后期上传

      本文链接:https://www.haomeiwen.com/subject/omajjftx.html