美文网首页
ios crash日志上传服务器

ios crash日志上传服务器

作者: 扛支枪 | 来源:发表于2019-03-28 19:52 被阅读0次
    • 首先在- (BOOL)application:(UIApplication *)application
      didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中添加这句:
    NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);
    
    • 然后在AppDelegate中添加方法:
    void UncaughtExceptionHandler(NSException *exception){
        NSArray *callStackArr = [exception callStackSymbols];
        NSString *reason = [exception reason];
        NSString *name = [exception name];
        NSMutableString *strSymbols = [NSMutableString string];
        
        for (int i = 0; i < callStackArr.count; i ++) {
            [strSymbols appendString:callStackArr[i]];
            [strSymbols appendString:@"\r\n"];
        }
        
        UIDevice *device = [UIDevice currentDevice];
        NSString *deInfo = [NSString stringWithFormat:@"%@,%@%@",
                            device.model,device.systemName,device.systemVersion];
        
        NSString *filePath = [NSHomeDirectory()
                              stringByAppendingPathComponent:@"Library/Caches/ExceptionLog"];
        NSString *crashString = [NSString stringWithFormat:@"DeviceInfo:%@\nExceptionName:%@\nExceptionReason:%@\nExceptionCallStackInfo:%@",deInfo,name,reason,strSymbols];
        [crashString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"崩溃详情CRASH: %@", exception);
        NSLog(@"崩溃详情Stack Trace: %@",[exception callStackSymbols]);
        [[CrashLogSender shareInstance] sendCrashLog:crashString];
    }
    
    • 其中CrashLogSender是发送请求的单例,代码如下:
    @implementation CrashLogSender
    + (instancetype)shareInstance{
        static CrashLogSender *crashLog;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            crashLog = [[CrashLogSender alloc] init];
        });
        return crashLog;
    }
    // 通过post 或者 get 方式来将异常信息发送到服务器
    - (void)sendCrashLog:(NSString *)crashLogDic {
        dispatch_semaphore_t semophore = dispatch_semaphore_create(0); // 创建信号量
        NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:nil delegateQueue:nil];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"你的上传日志的地址"]];
        [request setHTTPMethod:@"POST"];
        NSString *requestString = [NSString stringWithFormat:@"machine_code=11111111&crash_log=%@",crashLogDic];
        request.HTTPBody = [requestString dataUsingEncoding:NSUTF8StringEncoding];
        [[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            NSLog(@"response%@",response);
            dispatch_semaphore_signal(semophore); // 发送信号
    
        }] resume];
        dispatch_semaphore_wait(semophore, DISPATCH_TIME_FOREVER); // 等待
    }
    
    • 这个过程中遇到个问题,由于我集成有bugly,所以刚开始的时候UncaughtExceptionHandler方法一直没有调用,后来发现我设置NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler)是在bugly设置的之前,可能是bugly覆盖了(有空查下bugly实现原理),所以改成先让bugly设置,然后设置NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler)即可。
    • 参考的有别人的代码,忘记出处了,见谅
    • 还有一种方法是崩溃的时候把崩溃信息存本地,然后下次进app的时候判断本地数据是否存在,存在就上传,成功后删除即可。

    相关文章

      网友评论

          本文标题:ios crash日志上传服务器

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