iOS之奔溃记录

实在是不知道该写点什么,就写下如何记录程序的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 原创出品,欢迎转载,转载时请注明出处!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,482评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,986评论 19 139
  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,776评论 0 9
  • 早晨一书友微信发来一张写生照片——《端午清闲写绿萝》。他说:没事的时候,写生几下,自己养的花花草草,也蛮好玩的。他...
    花缘过客阅读 203评论 0 0
  • 【快板词】 咱呐忆那个往昔 峥嵘岁月 徐特立立志教育办长师 几多风雨几多愁 排除万难和险阻 一路向前不回头 不回头...
    椰子吃萱萱了阅读 235评论 0 1