捕捉错误日志 向服务器上传错误日志

捕捉异常类头文件.h文件

///抓捕异常处理

void uncaughtExceptionHandler(NSException *exception);

捕捉异常实现文件.m文件

///抓捕异常处理

void uncaughtExceptionHandler(NSException *exception)

{

    //手机型号

    NSString *deviceName = [DeviceTool getPhoneModel];

    //手机系统版本

    NSString *deviceSystemVersion = [DeviceTool getSystemVersion];

    //App 版本号

    NSString *appVersion = [DeviceTool getAppVersion];

    

    // 异常的堆栈信息

    NSArray *stackArray = [exception callStackSymbols];

    

    // 出现异常的原因

    NSString *reason = [exception reason];

    

    // 异常名称

    NSString *name = [exception name];

    

    NSString *exceptionInfo = [NSString stringWithFormat:@"Device name:%@ SystemVersion:%@  AppVersion:%@ \nException reason:%@\nException name:%@\nException stack:%@",deviceName,deviceSystemVersion,appVersion,name, reason, stackArray];

    

    NSMutableArray *tmpArr = [NSMutableArray arrayWithArray:stackArray];

    

    [tmpArr insertObject:reason atIndex:0];

    

    //保存到本地  --  当然你可以在下次启动的时候,上传这个log

    NSLog(@"错误日志路径%@",Crash_Error_Path);

    [exceptionInfo writeToFile:Crash_Error_Path  atomically:YES encoding:NSUTF8StringEncoding error:nil];

    //在这里发送网络请求发送错误日志字符串,从服务器后台就可以看到错误日志

    [self requestServerError];

}

 

//记得在应用程序启动时初始化 在(appDelegate.m)

/*抓捕异常的处理*/

    NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);

 

posted @ 2018-01-03 15:17  tryFighting  阅读(2237)  评论(0编辑  收藏  举报