zoukankan      html  css  js  c++  java
  • 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; }

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

  • 相关阅读:
    C/C++程序员必备的15个编辑器和集成开发环境
    天猫浏览型应用的CDN静态化架构演变
    实用技巧:如何用 CSS 做到完全垂直居中
    JavaScript 字符串操作:substring, substr, slice
    Hybrid App 开发初探:使用 WebView 装载页面
    引领潮流!最顶尖的24个获奖网页作品
    HTML5编程之旅系列一:HTML5 Geolocation 初探
    JavaScript 秘密花园——对象的使用和属性操作
    提高效率!15款最好的 Bug 跟踪应用程序
    最常用的8款 PHP 调试工具,你用过吗?
  • 原文地址:https://www.cnblogs.com/Lxiaolong/p/4172842.html
Copyright © 2011-2022 走看看