iOS 獲取崩潰日誌
阿新 • • 發佈:2018-11-02
新建一個類CatchCrash
@interface CatchCrash : NSObject
void uncaughtExceptionHandler(NSException *exception);
@end
@implementation CatchCrash void uncaughtExceptionHandler(NSException *exception){ NSArray *stackArry= [exception callStackSymbols]; NSString *reason = [exception reason]; NSString *name = [exception name]; NSString *exceptionInfo = [NSString stringWithFormat:@"Exception name:%@\nException reatoin:%@\nException stack :%@",name,reason,stackArry]; NSLog(@"exceptionInfo------>"); NSLog(@"%@",exceptionInfo); //儲存到本地沙盒中 [exceptionInfo writeToFile:[NSString stringWithFormat:@"%@/Documents/error.log",NSHomeDirectory()] atomically:YES encoding:NSUTF8StringEncoding error:nil]; } @end
AppDelegate匯入標頭檔案,在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中加入如下處理,獲取log檔案之後就可以走介面提交到後臺了,在後臺檢視相關日誌。
//註冊訊息處理函式的處理方法 NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler); // 傳送崩潰日誌 NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *dataPath = [path stringByAppendingPathComponent:@"error.log"]; NSData *data = [NSData dataWithContentsOfFile:dataPath]; if (data != nil) { //上傳log }