異常統計- IOS 收集崩潰資訊 NSException類
阿新 • • 發佈:2018-11-07
首先我們看下NSException
NSException是什麼?
最熟悉的陌生人,這是我對NSException的概述,為什麼這麼說呢?其實很多開發者接觸到NSException的頻率非常頻繁,但很多人都不知道什麼是NSException,不知道如何使用NSException。下面從一張截圖開始講起NSException。
直接上程式碼!
// // MyUncaughtExceptionHandler.h // RuiYang // // Created by Mac on 2018/7/20. // Copyright © 2018年 xiangxx. All rights reserved. // #import <Foundation/Foundation.h> // 崩潰日誌 @interface MyUncaughtExceptionHandler : NSObject + (void)setDefaultHandler; + (NSUncaughtExceptionHandler *)getHandler; + (void)TakeException:(NSException *) exception; @end
// // MyUncaughtExceptionHandler.m // RuiYang // // Created by Mac on 2018/7/20. // Copyright © 2018年 xiangxx. All rights reserved. // #import "MyUncaughtExceptionHandler.h" // 返回沙盒地址 NSString * applicationDocumentsDirectory() { return [NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES ) firstObject]; } // 出現崩潰時的回撥函式 void UncaughtExceptionHandler(NSException * exception) { NSArray * arr = [exception callStackSymbols]; // 崩潰的原因 可以有崩潰的原因(陣列越界,字典nil,呼叫未知方法...) 崩潰的控制器以及方法 NSString * reason = [exception reason]; NSString * name = [exception name]; NSString * url = [NSString stringWithFormat:@"========異常錯誤報告========\nname:%@\nreason:\n%@\ncallStackSymbols:\n%@",name,reason,[arr componentsJoinedByString:@"\n"]]; NSString * path = [applicationDocumentsDirectory() stringByAppendingPathComponent:@"ExceptionLog.txt"]; // 將txt檔案寫入沙盒 [url writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil]; } @implementation MyUncaughtExceptionHandler + (void)setDefaultHandler { NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler); } + (NSUncaughtExceptionHandler *)getHandler { return NSGetUncaughtExceptionHandler(); } + (void)TakeException:(NSException *)exception { NSArray * arr = [exception callStackSymbols]; NSString * reason = [exception reason]; NSString * name = [exception name]; NSString * url = [NSString stringWithFormat:@"========異常錯誤報告========\nname:%@\nreason:\n%@\ncallStackSymbols:\n%@",name,reason,[arr componentsJoinedByString:@"\n"]]; NSString * path = [applicationDocumentsDirectory() stringByAppendingPathComponent:@"ExceptionLog.txt"]; [url writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil]; } @end
#import <Foundation/Foundation.h>
@interface StatisRequestErrorData : NSObject
+ (void)showXcodeInfo;
@end
#import "StatisRequestErrorData.h" #import "MyUncaughtExceptionHandler.h" @implementation StatisRequestErrorData #pragma mark -- 崩潰日誌 + (void)showXcodeInfo{ [MyUncaughtExceptionHandler setDefaultHandler]; // 傳送崩潰日誌 NSString *path = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES ) firstObject]; NSString *dataPath = [path stringByAppendingPathComponent:@"ExceptionLog.txt"]; NSData *data = [NSData dataWithContentsOfFile:dataPath]; if (data != nil) { [StatisRequestErrorData sendExceptionLogWithData:data]; }else{ NSLog(@"沒有崩潰日誌"); } } #pragma mark -- 傳送崩潰日誌 + (void)sendExceptionLogWithData:(NSData *)data { NSLog(@"======資料上傳奔潰日誌成功========="); NSString * path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"ExceptionLog.txt"]; [StatisRequestErrorData removeDocumentWithFilePath:path]; } // 刪除本地奔潰日誌 + (BOOL)removeDocumentWithFilePath:(NSString*)filePath{ BOOL isRemove = false; NSFileManager* fileManager=[NSFileManager defaultManager]; if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) { isRemove = [fileManager removeItemAtPath:filePath error:nil]; } return isRemove; } @end
在AppDelegate中
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//統計計奔潰日誌
[StatisRequestErrorData showXcodeInfo];
return YES;
}
reason 是異常的原因
name 是異常的名稱