捕捉錯誤日志 向服務器上傳錯誤日志


捕捉異常類頭文件.h文件

///抓捕異常處理

void uncaughtExceptionHandler(NSException *exception);

捕捉異常實現文件.m文件

///抓捕異常處理

void uncaughtExceptionHandler(NSException *exception)

{

    //手機型號

    NSString *deviceName = [DeviceTool getPhoneModel];

    //手機系統版本

    NSString *deviceSystemVersion = [DeviceTool getSystemVersion];

    //App 版本號

    NSString *appVersion = [DeviceTool getAppVersion];

    

    // 異常的堆棧信息

    NSArray *stackArray = [exception callStackSymbols];

    

    // 出現異常的原因

    NSString *reason = [exception reason];

    

    // 異常名稱

    NSString *name = [exception name];

    

    NSString *exceptionInfo = [NSString stringWithFormat:@"Device name:%@ SystemVersion:%@  AppVersion:%@ \nException reason:%@\nException name:%@\nException stack:%@",deviceName,deviceSystemVersion,appVersion,name, reason, stackArray];

    

    NSMutableArray *tmpArr = [NSMutableArray arrayWithArray:stackArray];

    

    [tmpArr insertObject:reason atIndex:0];

    

    //保存到本地  --  當然你可以在下次啟動的時候,上傳這個log

    NSLog(@"錯誤日志路徑%@",Crash_Error_Path);

    [exceptionInfo writeToFile:Crash_Error_Path  atomically:YES encoding:NSUTF8StringEncoding error:nil];

    //在這里發送網絡請求發送錯誤日志字符串,從服務器后台就可以看到錯誤日志

    [self requestServerError];

}

 

//記得在應用程序啟動時初始化 在(appDelegate.m)

/*抓捕異常的處理*/

    NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM