iOS记录崩溃信息

需求

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

解决

       oc自带函数,能够监控到app发生异常
函数

      NSSetUncaughtExceptionHandler(&catchLog);测试

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

走起

    1.CatchLog.h    我使用了ASIHttpRequestspa

#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中,写入代码code

 [CatchLog catchExceptionLog];

    3.测试代码可使用
orm

[NSDictionary dictionaryWithObject:nil forKey:nil];
相关文章
相关标签/搜索