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];
相關文章
相關標籤/搜索