iOS程序崩溃日志

来源:互联网 发布:淘宝卖家如何添加客服 编辑:程序博客网 时间:2024/05/19 14:20

还记得某一位大神曾经说过,没有不存在bug的代码,然而为觉得,不存在不回崩溃的程序。而程序崩溃的原因有很多种,最简单的,比如你的数组越界了,比如你的对象被释放了,比如你的内存爆炸了,很多很多的原因,这个时候,我们就需要用到崩溃日志的收集 下面就贴上代码: 在appDelegate中添加如下方法

void UncaughtExceptionHandler(NSException exception) {NSArray array = [exception callStackSymbols];//当前的调用信息(包含崩溃地址) NSString reason = [exception reason];//崩溃的原因 NSString name = [exception name];//异常类型 NSDictionary *dic = @{@"name":name,@"reason":reason,@"array":arr}; }

当然你还要在以下方法中添加调用

(BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions { NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);//错误日志收集}

下面时数组越界时返回的错误日志

arr = (    "0   CoreFoundation                      0x0000000182a69918 <redacted> + 148",    "1   libobjc.A.dylib                     0x00000001820d7f80 objc_exception_throw + 56",    "2   CoreFoundation                      0x000000018294fac4 CFRunLoopRemoveTimer + 0",    "3   你的程序               0x0000000100095d00 -[SecViewController viewDidLoad] + 1124",崩溃的具体地点    "4   UIKit                               0x000000018775c0c0 <redacted> + 996",    "5   UIKit                               0x000000018775bcc4 <redacted> + 28",    "6   UIKit                               0x0000000188026ca0 <redacted> + 92",    "7   UIKit                               0x0000000187ab30dc <redacted> + 136",    "8   UIKit                               0x0000000187ad8c24 <redacted> + 3780",    "9   UIKit                               0x0000000187adb9c0 <redacted> + 472",    "10  UIKit                               0x0000000187855cec <redacted> + 184",    "11  UIKit                               0x0000000187e946f0 <redacted> + 252",    "12  UIKit                               0x0000000187e8763c <redacted> + 456",    "13  UIKit                               0x0000000187e87440 <redacted> + 92",    "14  UIKit                               0x0000000187e87708 <redacted> + 160",    "15  UIKit                               0x0000000187793e50 <redacted> + 100",    "16  UIKit                               0x0000000187793dcc <redacted> + 80",    "17  UIKit                               0x000000018777ba88 <redacted> + 416",    "18  UIKit                               0x00000001877936e4 <redacted> + 572",    "19  UIKit                               0x0000000187793314 <redacted> + 804",    "20  UIKit                               0x000000018778be30 <redacted> + 784",    "21  UIKit                               0x000000018775c4cc <redacted> + 248",    "22  UIKit                               0x000000018775a794 <redacted> + 5528",    "23  CoreFoundation                      0x0000000182a20efc <redacted> + 24",    "24  CoreFoundation                      0x0000000182a20990 <redacted> + 540",    "25  CoreFoundation                      0x0000000182a1e690 <redacted> + 724",    "26  CoreFoundation                      0x000000018294d680 CFRunLoopRunSpecific + 384",    "27  GraphicsServices                    0x0000000183e5c088 GSEventRunModal + 180",    "28  UIKit                               0x00000001877c4d90 UIApplicationMain + 204",    "29  zhongrongjinfuBeta                  0x0000000100098548 main + 124",    "30  libdyld.dylib                       0x00000001824ee8b8 <redacted> + 4");
name = NSRangeException;崩溃的类型崩溃的具体原因reason = "*** -[__NSArrayI objectAtIndex:]: index 4 beyond bounds [0 .. 2]";现在崩溃日志已经收集到了,我们要做什么处理呢有两个选择:一:上传到服务器 把信息崩溃的信息存储为字典,上传到服务器 代码如下:NSMutableURLRequest requset = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"上传到的地址"]]; NSData jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil]; requset.HTTPBody = jsonData; requset.HTTPMethod = @"POST"; // 发送请求[NSURLConnection sendAsynchronousRequest:requset queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {            if (data) {                    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];                NSLog(@"%@", dict);                } else {                     NSLog(@"上传失败");                     }         }];

二:存储到本地

NSString path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject; NSString fullPath = [path stringByAppendingString:@"/crash.plist"];///crash.plist存储的格式和名称“/”表示存储到目录中 [dic writeToFile:fullPath atomically:NO];读取文件: NSDictionary *dicc = [[NSDictionary alloc]initWithContentsOfFile:fullPath];//fullPath存储的地址

现在也有一些第三方服务提供错误日志的收集,而且苹果本身也有这个服务。所以现在更多的侧重点就放在了错误日志的分析上,至于错误日志的分析,以后在做分析

1 0