iOS 捕獲程序崩潰日誌

iOS開發中遇到程序崩潰是很正常的事情,如何在程序崩潰時捕獲到異常信息並通知開發者?函數

下面就介紹如何在iOS中實現:指針

1. 在程序啓動時加上一個異常捕獲監聽,用來處理程序崩潰時的回調動做
orm

NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);

官方文檔介紹:Sets the top-level error-handling function where you can perform last-minute logging before the program terminates.
UncaughtExceptionHandler是一個函數指針,該函數須要咱們實現,能夠取本身想要的名字。當程序發生異常崩潰時,該函數會獲得調用,這跟C,C++中的回調函數的概念是同樣的。

2. 實現本身的處理函數
blog

void UncaughtExceptionHandler(NSException *exception) {
    NSArray *arr = [exception callStackSymbols];//獲得當前調用棧信息
    NSString *reason = [exception reason];//很是重要,就是崩潰的緣由
    NSString *name = [exception name];//異常類型
   
    NSLog(@"exception type : %@ \n crash reason : %@ \n call stack info : %@", name, reason, arr);
}

以上代碼很簡單,可是帶來的做用是很是大的。
開發

相關文章
相關標籤/搜索