• ios异常(crash)输出


    最近突然想起友盟的sdk附带的一个功能:将闪退异常情况上报服务器,(stackflow,github)找了一些资料,自己写了一个demo,想起来好久没有写过blog了,顺便分享。

    其实不止是ios,android逻辑也是一样的,crash日志其实是系统自带的,闪退的时候,都会将crash打印,使用ide的同学可以很明显的调试看到错误的信息,定位问题。

    程序打包给用户后,我们想查看程序运行情况的话,都是通过将这些crash log写入文件中(来不及上传,建立一个链接的),在下次启动的时候顺便传送服务器就是了。

    简单的代码逻辑,就是建立一个try catch消息响应,进行处理。

    下面是演示的代码例子:

    //这里是主要的异常处理例子
    void
    uncaughtExceptionHandler(NSException *exception) { // 异常的堆栈信息 NSArray * stackArray = [exception callStackSymbols]; // 出现异常的原因 NSString * reason = [exception reason]; // 异常名称 NSString * name = [exception name]; NSString * exceptionInfo = [NSString stringWithFormat:@"Exception reason:%@/nException name:%@/nException stack:%@",name, reason, stackArray]; NSLog(@"%@", exceptionInfo); NSMutableArray * tmpArr = [NSMutableArray arrayWithArray:stackArray]; [tmpArr insertObject:reason atIndex:0]; //保存到本地 — 当然你可以在下次启动的时候,上传这个log [exceptionInfo writeToFile:[NSString stringWithFormat:@"%@/Documents/error.log",NSHomeDirectory()] atomically:YES encoding:NSUTF8StringEncoding error:nil]; } - (void) printError { NSError *error; NSString *textFileContents = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@/Documents/error.log",NSHomeDirectory()] encoding: NSUTF8StringEncoding error: & error]; if (textFileContents == nil) { NSLog(@"Error reading text file. %@", [error localizedFailureReason]); } NSArray *lines = [textFileContents componentsSeparatedByString:@" "]; NSLog(@"%@", textFileContents); NSLog(@"Number of lines in the file:%d", [lines count] ); }
    // 程序启动的时候直接调用
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 捕获异常 NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler); self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; [self.window makeKeyAndVisible]; self.window.rootViewController = [[ViewController alloc]init]; [[CatchCrash alloc] printError]; return YES; }

    基本的逻辑就是这样,代码也非常简单。

  • 相关阅读:
    Powershell 音乐播放
    Powershell指令集_2
    Powershell指令集_2
    Powershell 邮件发送
    Powershell 邮件发送
    Oracle 11g 关闭内存自动管理
    Oracle 11g 内存手动管理
    Oracle 内存参数调整
    RESTful三理解
    RESTful三理解
  • 原文地址:https://www.cnblogs.com/Lxiaolong/p/4172842.html
Copyright © 2020-2023  润新知