iOS之APP异常崩溃抓取

NSSetUncaughtExceptionHandler

自己用程序捕获 crash,保存于本地

  1. 新建一个继承自NSObject的类(Xcode新建一个空项目过程略),取名字CatchCrash,在h和m文件中写下:

     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];  
    }
  2. 添加一个继承自UIViewcontroller的类,取名字为TestViewController。

  3. 注册CatchCrash异常处理方法,在Appdelegate写下如下代码:

     - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
    {  
     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
     // Override point for customization after application launch.  
    
     //注册消息处理函数的处理方法  
     NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);  
    
     TestViewController *testVc = [[TestViewController alloc] init];  
     self.window.rootViewController = testVc;  
    
     self.window.backgroundColor = [UIColor whiteColor];  
     [self.window makeKeyAndVisible];  
     return YES;  
    }
  4. 崩溃闪退后,下次启动 app 时上传该 log 文件内容至服务器即可。

增强特性

NSSetUncaughtExceptionHandler 用来做异常处理,但功能非常有限.

而引起崩溃的大多数原因如:内存访问错误,重复释放等错误就无能为力了,因为这种错误它抛出的是Signal,所以必须要专门做Signal处理

  1. 定义 UncaghtExceptionHandler 类,h 文件:

     @interface UncaughtExceptionHandler : NSObject{
         BOOL dismissed;
     }
    
     void InstallUncaughtExceptionHandler();
     @end

    m 文件:

    ```
    void InstallUncaughtExceptionHandler()
    {

      signal(SIGABRT, MySignalHandler);
     signal(SIGILL, MySignalHandler);
     signal(SIGSEGV, MySignalHandler);
     signal(SIGFPE, MySignalHandler);
     signal(SIGBUS, MySignalHandler);
     signal(SIGPIPE, MySignalHandler);

    }

//当 app 发生错误产生如上 signal 后,即会回掉自定义函数MySignalHandler
具体实现略

```
  1. 在didFinishLaunchingWithOptions中调用该函数:

     - (void)installUncaughtExceptionHandler
         {
             InstallUncaughtExceptionHandler();
         }

综上2步:所有崩溃基本上没问题了。

获取 crash 闪退日志

  1. XCode 的菜单Window->Organizer 选择Devices -> 选中的手机 -> 点击手机名称左边的箭头

    Unknown和Crash 这两种类型分别是 内存不够回收内存kill应用程序导致Crash和程序异常Crash的日志。

  2. 打开手机 - > 设置 -> 隐私 - > 诊断与用量 - > 诊断与用量数据 这里面就是所有应用的Crash日志。

    先找到存放crash的iphone系统路径:var/mobile/Library/Logs/CrashReporter)找到了crash存放的路径,唉,苦于无法读取(用程序读出来都是nil)

  3. 通过iTunes Connect(Manage Your Applications - View Details - Crash Reports)获取用户的crash日志。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值