iOS之奔溃记录

作者: CrazySteven | 来源:发表于2017-01-24 10:48 被阅读507次

实在是不知道该写点什么,就写下如何记录程序的Crash吧。

原理很简单,就是创建一个Crash的管理对象,然后记录错误,每次启动的时候判断是否有错误记录,如果有错误记录就处理错误记录。下面是具体实现过程:

1.创建一个NSobject对象

.h文件中写三个方法:

+ (void)setDefaultHandler;
+ (NSUncaughtExceptionHandler *)getHandler;
+ (void)TakeException:(NSException *) exception;

.m文件中进行实现:

// 沙盒的地址
NSString * applicationDocumentsDirectory() {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

// 崩溃时的回调函数
void UncaughtExceptionHandler(NSException * exception) {
    NSArray * arr = [exception callStackSymbols];
    NSString * reason = [exception reason]; // // 崩溃的原因
    NSString * name = [exception name];
    NSString * url = [NSString stringWithFormat:@"========异常错误报告========\nname:%@\nreason:\n%@\ncallStackSymbols:\n%@",name,reason,[arr componentsJoinedByString:@"\n"]];
    NSString * path = [applicationDocumentsDirectory() stringByAppendingPathComponent:@"Exception.txt"];
    // 将一个txt文件写入沙盒
    [url writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
}

+ (void)setDefaultHandler {
    NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);
}

+ (NSUncaughtExceptionHandler *)getHandler {
    return NSGetUncaughtExceptionHandler();
}

+ (void)TakeException:(NSException *)exception {
    NSArray * arr = [exception callStackSymbols];
    NSString * reason = [exception reason];
    NSString * name = [exception name];
    NSString * url = [NSString stringWithFormat:@"========异常错误报告========\nname:%@\nreason:\n%@\ncallStackSymbols:\n%@",name,reason,[arr componentsJoinedByString:@"\n"]];
    NSString * path = [applicationDocumentsDirectory() stringByAppendingPathComponent:@"Exception.txt"];
    [url writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
}

2.每次启动程序的时候判断是否有错误记录,如果有就处理,处理后删除文件

didFinishLaunchingWithOptions方法中实现:

    [CrashLogUpdate setDefaultHandler];
    // 查看是否有奔溃日志
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *dataPath = [path stringByAppendingPathComponent:@"Exception.txt"];
    NSData *data = [NSData dataWithContentsOfFile:dataPath];
    if (data != nil) {
        [self sendExceptionLogWithData:data path:dataPath];
    }

sendExceptionLogWithData处理并删除:

     NSString * string = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
     
     NSLog(@"%@",string);
     
     // 删除文件
     NSFileManager *fileManger = [NSFileManager defaultManager];
     [fileManger removeItemAtPath:path error:nil];

以上两部就实现了全部过程,很简单,现在也有很多第三方库,并且还用了Swizzle来记录用户的操作步骤,还对crash进行了分析,给出了建议的解决方案等等超多功能,智能的不要不要的,我们就可以不用再造轮子了,拿来用吧-
demo点击下载

版权声明:本文为 Crazy Steven 原创出品,欢迎转载,转载时请注明出处!

相关文章

  • iOS之奔溃记录

    实在是不知道该写点什么,就写下如何记录程序的Crash吧。 原理很简单,就是创建一个Crash的管理对象,然后记录...

  • iOS奔溃日志分析

    iOS奔溃日志分析 前言(扯淡) iOS奔溃日志能够比较有效的分析奔溃的原因,方便我们debug我们的项目。当然现...

  • DYLD, Library not loaded: /usr/l

    奔溃日志 奔溃表现:iOS12.1 及以下启动奔溃奔溃日志: 解决方法:关闭bitcode,重新打包上传appst...

  • iOS13 textfield的placeholder字体颜色崩

    由于iOS13禁止了textfield通过KVC获取私有属性,出现奔溃问题 奔溃报错Access to UITex...

  • iOS 记录使用IDA和奔溃信息定位奔溃代码

    首先在奔溃日志中确定是在哪一条线程奔溃的,然后找到我们工程名所在的第一条日志地址。从图片中可以看出是第0个线程,然...

  • iOS收集Crash信息上报

    在iOS开发中,最严重的bug估计就是应用奔溃,如果应用奔溃了,除了做好挨骂的准备,还需要冷静的下来去处理这个事情...

  • iOS 9.x assets 导致的奔溃问题

    问题描述:上周新上传的版本在Bug收集中发现有几个地方出现很多次奔溃问题,问题都集中在iOS 9.x中奔溃机型分布...

  • iOS-千奇百怪的奔溃

    App 上线后,我们最怕的应该就是异常奔溃了。常见的奔溃类型分两种:信号可捕获奔溃、信号不可捕获奔溃,前者比较典型...

  • 奔溃

    送表弟们去上学的路上,听说刚才忘了拿《斗罗大陆》,却又接着说可以向同学借。我的内心立即奔溃了,满满的无力感! 当初...

  • 奔溃

    我总是想不让自己在乎别人的眼光,所以我总是在街上很自在,不担心自己的丑态被别人看到,我也经常对女友这么说,不要在意...

网友评论

本文标题:iOS之奔溃记录

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