iOS崩潰 捕獲異常處理

2021-08-08 18:13:01 字數 1923 閱讀 9008

之前部落格介紹啦一種異常捕獲後彈窗提示使用者的方法,《ios崩潰 捕獲異常處理》,下面提供另外一種異常捕獲的方法。原理一致,但實現略有不同。可供參考。

1、在didfinishlaunchingwithoptions 中,註冊訊息處理函式,處理崩潰資訊,寫入本地。

//註冊訊息處理函式的處理方法,處理崩潰資訊,寫入本地

nssetuncaughtexceptionhandler(&uncaughtexceptionhandler);

2、將crashmanager.h、 crashmanager.m 拖拽到工程中,具體實現如下: 

crashmanager.h:

@inte***ce crashmanager : nsobject

/** 捕捉crash */

void uncaughtexceptionhandler(n***ception *exception);

+ (id)defaultmanager;//單例

/** 移除crash的log日誌 */

- (void)clearcrashlog;

/** 是否有log日誌 */

- (bool)iscrashlog;

/** crash log日誌 */

- (nsstring *)crashlogcontent;

@end

crashmanager.m 中核心函式如下:

void uncaughtexceptionhandler(n***ception *exception)
+ (id)defaultmanager);

}return crashmanager;

}#pragma mark -移除crash的log日誌

- (void)clearcrashlog

#pragma mark - 是否有log日誌

- (bool)iscrashlogelse

}#pragma mark -crash log日誌

- (nsstring *)crashlogcontentelse

}#pragma mark - 檢查是否有空字元

- (bool)checkconvertnull:(nsstring *)objectelse

}

3、在viewcontroller中,做如下測試案例:

- (void)viewdidload 

// [crashmanager clearcrashlog];//清除crash日誌

//crash測試

uibutton *crashbtn = [uibutton buttonwithtype:uibuttontypecustom];

crashbtn.frame = cgrectmake(self

.view

.frame

.size

.width/2 - 50, 200, 100, 40);

crashbtn.backgroundcolor = [uicolor redcolor];

[crashbtn addtarget:self action:@selector(crashtest) forcontrolevents:uicontroleventtouchupinside];

[crashbtn settitle:@"crash" forstate:uicontrolstatenormal];

[crashbtn settitlecolor:[uicolor blackcolor] forstate:uicontrolstatenormal];

[self

.view addsubview:crashbtn];

}-(void)crashtest;

}

IOS 異常捕獲

include include 系統訊號截獲處理方法 void signalhandler int signal 異常截獲處理方法 void exceptionhandler n ception exception const int32 t uncaughtexceptionmaximum 10 ...

ios異常捕獲

當然是在你出錯的控制器裡捕獲,如果你想全域性捕獲,那你就把捕獲方法寫在你的根控制器裡,比如baseviewcontroller.m裡面 兩段 量很少噠 第一處 寫在viewdidload裡面 nssetuncaughtexceptionhandler uncaughtexceptionhandler...

IOS 異常捕獲

include include 系統訊號截獲處理方法 void signalhandler int signal 異常截獲處理方法 void exceptionhandler n ception exception const int32 t uncaughtexceptionmaximum 10 ...