iOS记录崩溃信息

需求

        需要知道APP闪退的时候,到底发生了什么

解决

       oc自带函数,可以监控到app发生异常

      NSSetUncaughtExceptionHandler(&catchLog);

        catchLog为C语言函数,传入函数指针,当发生异常时候,可以捕捉

走起

    1.CatchLog.h    我使用了ASIHttpRequest

#import <Foundation/Foundation.h>
#import "ASIHTTPRequest.h"

@interface CatchLog : NSObject <ASIHTTPRequestDelegate>

+ (void)catchExceptionLog;

+ (void)uploadTheLog;

@end

     CatchLog.m

    

#import "CatchLog.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"

@implementation CatchLog

void catchLog(NSException *exception){
    NSLog(@"捕捉到了异常");
    NSArray *arr = [exception callStackSymbols];//得到当前调用栈信息
    NSString *reason = [exception reason];//非常重要,就是崩溃的原因
    NSString *name = [exception name];//异常类型
    
    DDLogError(@"异常类型:%@",name);
    DDLogError(@"崩溃原因:%@",reason);
    DDLogError(@"栈信息:%@",arr);
    
    //设置标志代表这次错误信息,需要下次打开APP时上传
    [[NSUserDefaults standardUserDefaults] setObject:@YES forKey:@"isUploadLog"];
}

+ (void)catchExceptionLog{
    NSSetUncaughtExceptionHandler(&catchLog);
}

#pragma mark --上传日志
+ (void)uploadTheLog{
    NSURL *url = [NSURL URLWithString:@"http://myapp.tunnel.mobi/app/uploadLog"];
    NSString *filePath = [[NSUserDefaults standardUserDefaults]objectForKey:@"logPath"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//    [request setValue:@"123" forKey:@"logContent"];
    [request setPostValue:str forKey:@"logContent"];
//    [request setFile:filePath forKey:@"logFile"];
//    request.delegate = self;
//    [request setDidFinishSelector:@selector(responseComplete)];
    [request setCompletionBlock:^{
        NSLog(@"上传成功");
    }];
    [request startSynchronous];
}

@end

    filePath路径从本地取出,log日志具体可参参考上篇日志

    2. 在AppDelegate中didFinishLaunchingWithOptions中,写入代码

 [CatchLog catchExceptionLog];

    3.测试代码可以使用

[NSDictionary dictionaryWithObject:nil forKey:nil];

    

转载于:https://my.oschina.net/u/2417622/blog/496623

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值