美文网首页iOS开发精华专题iOS deviOS技术专题
iOS开发中捕获程序崩溃日志报告发送至邮件

iOS开发中捕获程序崩溃日志报告发送至邮件

作者: Courage_SC | 来源:发表于2016-04-12 13:25 被阅读723次

    iOS开发中遇到程序崩溃是很正常的事情,如何在程序崩溃时捕获到异常信息并通知开发者,是大多数软件都选择的方法。下面就介绍如何在iOS中实现:

    1. 在程序启动时加上一个异常捕获监听,用来处理程序崩溃时的回调动作
      NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);
    

    官方文档介绍:Sets the top-level error-handling function where you can perform last-minute logging before the program terminates.
    UncaughtExceptionHandler是一个函数指针,该函数需要我们实现,可以取自己想要的名字。当程序发生异常崩溃时,该函数会得到调用,这跟C,C++中的回调函数的概念是一样的。

    1. 实现自己的处理函数
    void UncaughtExceptionHandler(NSException *exception) {
        NSArray *arr = [exception callStackSymbols];//得到当前调用栈信息
        NSString *reason = [exception reason];//非常重要,就是崩溃的原因
        NSString *name = [exception name];//异常类型
       
        NSLog(@"exception type : %@ \n crash reason : %@ \n call stack info : %@", name, reason, arr);
    }
    

    以上代码很简单,但是带来的作用是非常大的。

    获取到了崩溃的日子,如何发送给开发者呢,目前一般有以下两种方式:

    1. 将崩溃信息持久化在本地,下次程序启动时,将崩溃信息作为日志发送给开发者。

    2. 通过邮件发送给开发者。 不过此种方式需要得到用户的许可,因为iOS不能后台发送短信或者邮件,会弹出发送邮件的界面,只有用户点击了发送才可发送。 不过,此种方式最符合苹果的以用户至上的原则。
      发送邮件代码也很简单:

     NSString *crashLogInfo = [NSString stringWithFormat:@"exception type : %@ \n crash reason : %@ \n call stack info : %@", name, reason, arr];
        NSString *urlStr = [NSString stringWithFormat:@"mailto://mailto://__@163.com?subject=bug报告&body=感谢您的配合!错误详情:%@",crashLogInfo];
        NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        [[UIApplication sharedApplication] openURL:url];
    

    以上就是iOS中捕获异常常用的方法,大家可以不妨一试!

    相关文章

      网友评论

      • Courage_SC:哪位大神抄袭我的博客, 发的BUG邮箱没改, 是我的邮箱:sweat: 麻烦改一下
      • 陌上北辰:崩溃的日志发到邮箱,你亲测过么,我发了邮箱收不到? 你是真机还是模拟器
      • Courage_SC:你的崩溃日志能打印出来吗? 我是将崩溃日志发到邮箱了
      • 深山问:请问你是怎么将奔溃设备的信息写入文件中的?[[NSBundle mainBundle] infoDictionary]中的信息,writeToFile总是失败。
        深山问:额,为什么回复和评论功能不支持markdown :sweat:
        深山问:可以。我的想法是搜集这些崩溃信息保存在本地,程序下次启动时上传到服务器,便于热修复。我试了一下,将[[NSBundle mainBundle] infoDictionary]writeToFile会失败,但是我截取部分字段后可以保存成功。代码如下:
        ```
        + (BOOL)writeCrashFileOnDocumentsException:(NSDictionary *)exception{
        NSString *time = [[NSDate date] formattedDateWithFormat:@"yyyyMMddHHmmss" locale:[NSLocale currentLocale]];
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
        NSString *crashname = [NSString stringWithFormat:@"%@_%@Crashlog.plist",time,infoDictionary[@"CFBundleName"]];
        NSString *crashPath = [[self sd_getCachesPath] stringByAppendingPathComponent:SDCrashFileDirectory];
        NSFileManager *manager = [NSFileManager defaultManager];
        //设备信息
        NSMutableDictionary *deviceInfos = [NSMutableDictionary dictionary];
        [deviceInfos setObject:[infoDictionary objectForKey:@"DTPlatformVersion"] forKey:@"DTPlatformVersion"];
        [deviceInfos setObject:[infoDictionary objectForKey:@"CFBundleShortVersionString"] forKey:@"CFBundleShortVersionString"];
        [deviceInfos setObject:[infoDictionary objectForKey:@"UIRequiredDeviceCapabilities"] forKey:@"UIRequiredDeviceCapabilities"];

        BOOL isSuccess = [manager createDirectoryAtPath:crashPath withIntermediateDirectories:YES attributes:nil error:nil];
        if (isSuccess) {
        NSLog(@"文件夹创建成功");
        NSString *filepath = [crashPath stringByAppendingPathComponent:crashname];
        NSMutableDictionary *logs = [NSMutableDictionary dictionaryWithContentsOfFile:filepath];
        if (!logs) {
        logs = [[NSMutableDictionary alloc] init];
        }
        //日志信息
        NSDictionary *infos = @{@"Exception":exception,@"DeviceInfo":deviceInfos};
        [logs setObject:infos forKey:[NSString stringWithFormat:@"%@_crashLogs",infoDictionary[@"CFBundleName"]]];
        BOOL writeOK = [logs writeToFile:filepath atomically:YES];
        NSLog(@"write result = %d,filePath = %@",writeOK,filepath);
        return writeOK;
        }else{
        return NO;
        }
        }
        ```
        Courage_SC:@默默desire 崩溃日志在控制能打印出来吗。我的是将崩溃日志发到邮箱了

      本文标题:iOS开发中捕获程序崩溃日志报告发送至邮件

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