iOS资料SDKiOS基本功

iOS收集崩溃日志SDK封装

2019-01-06  本文已影响345人  喵洛

接近年底,来了个比较急的客户端项目需求----做一个类似腾讯的MTA、bugly的搜集Crash的SDK,供其他app使用。

之前,我也看过MTA、bugly这些搜集Crash的SDK,现在需要自己来写一个类似的SDK,然后,我就自己研究了下,可以通过APP里的API方法(NSSetUncaughtExceptionHandler)实现,下面,我就来说下此方法的使用和我封装SDK的思路。

封装SDK最主要的部分总共两点:
1、搜集Crash信息
2、Crash信息上传到服务器

1、搜集Crash信息最主要的就是NSSetUncaughtExceptionHandler方法,只要在APP启动的时候注册此方法,并把搜集的信息记录下来即可,涉及到的其他逻辑请自行完成,下面是核心代码部分

NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);

void UncaughtExceptionHandler(NSException *exception)
{
    // 异常的堆栈信息
    NSArray *stackArray = [exception callStackSymbols];
    // 出现异常的原因
    NSString *reason = [exception reason];
    // 异常名称
    NSString *name = [exception name];
    // 异常发生时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    
    NSMutableDictionary *crashInfo = [NSMutableDictionary dictionary];
    [crashInfo setObject:name forKey:@"crashName"];
    [crashInfo setObject:dateTime forKey:@"crashTime"];
    [crashInfo setObject:reason forKey:@"crashReason"];
    [crashInfo setObject:[stackArray componentsJoinedByString:@"\n"] forKey:@"crashStack"];
    
    NSDictionary *appInfo = [CrashHandler getAppInfo];
    NSDictionary *deviceInfo = [CrashHandler getDeviceInfo];
    
    [crashInfo setValuesForKeysWithDictionary:appInfo];
    [crashInfo setValuesForKeysWithDictionary:deviceInfo];
    
    NSMutableArray *logArray = [NSMutableArray arrayWithArray:[CrashHandler readCrashLog]];
    [logArray addObject:crashInfo];
    [CrashHandler writeCrashLog:logArray]z;
}

每次Crash的时候,把信息记录下来,下次APP启动的时候上传到服务端,因为有可能上传的时候失败,所以我这里使用的是数组存的Crash日志,当APP启动的时候,Crash上传成功后,就会把本地记录的日志删除。

2、Crash信息上传到服务器也是重要的一部操作,而且作为SDK,你的网络请求部分不能依赖第三方,虽然第三方的网络请求比较完善,所以,我们需要自己使用原生的网络库,自己封装请求。
这里我使用的iOS的NSURLSessionDataTask封装的,详细的封装,我这里就写个核心代码部分,其他的网上可以搜出一堆。

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
request.HTTPMethod = @"POST";
request.timeoutInterval = 15;
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
}];
[task resume];

然后,自己在界面上随便制造一个Crash,通过代码获取,可以看到Crash的内容。

crashName = NSRangeException;
crashReason = "*** -[__NSArray0 objectAtIndex:]: index 1 beyond bounds for empty NSArray";
crashStack = "0   CoreFoundation                      0x0000000101ee41e6 __exceptionPreprocess + 294
1   libobjc.A.dylib                     0x0000000101579031 objc_exception_throw + 48
2   CoreFoundation                      0x0000000101efce3d -[__NSArray0 objectAtIndex:] + 93
3   TestFramework                       0x0000000100c6c8a4 -[ViewController testBtnAction] + 100
4   UIKit                               0x00000001023623e8 -[UIApplication sendAction:to:from:forEvent:] + 83
5   UIKit                               0x00000001024dd7a4 -[UIControl sendAction:to:forEvent:] + 67
6   UIKit                               0x00000001024ddac1 -[UIControl _sendActionsForEvents:withEvent:] + 450
7   UIKit                               0x00000001024dca09 -[UIControl touchesEnded:withEvent:] + 580
8   UIKit                               0x00000001023d70bf -[UIWindow _sendTouchesForEvent:] + 2729
9   UIKit                               0x00000001023d87c1 -[UIWindow sendEvent:] + 4086
10  UIKit                               0x000000010237c310 -[UIApplication sendEvent:] + 352
11  UIKit                               0x0000000102cbd6af __dispatchPreprocessedEventFromEventQueue + 2796
12  UIKit                               0x0000000102cc02c4 __handleEventQueueInternal + 5949
13  CoreFoundation                      0x0000000101e86bb1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
14  CoreFoundation                      0x0000000101e6b4af __CFRunLoopDoSources0 + 271
15  CoreFoundation                      0x0000000101e6aa6f __CFRunLoopRun + 1263
16  CoreFoundation                      0x0000000101e6a30b CFRunLoopRunSpecific + 635
17  GraphicsServices                    0x000000010711aa73 GSEventRunModal + 62
18  UIKit                               0x0000000102361057 UIApplicationMain + 159
19  TestFramework                       0x0000000100c70a9f main + 111
20  libdyld.dylib                       0x0000000105941955 start + 1
21  ???                                 0x0000000000000001 0x0 + 1";
crashTime = "2019-01-05 15:23:16";
上一篇下一篇

猜你喜欢

热点阅读