#import "NdUncaughtExceptionHandler.h" NSString *applicationDocumentsDirectory() { return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject]; } void UncaughtExceptionHandler(NSException *exception) { NSArray *arr = [exception callStackSymbols]; NSString *reason = [exception reason]; NSString *name = [exception name]; NSString *url = [NSString stringWithFormat:@"=============異常崩潰報告=============\nname:\n%@\nreason:\n%@\ncallStackSymbols:\n%@", name,reason,[arr componentsJoinedByString:@"\n"]]; NSString *path = [applicationDocumentsDirectory()stringByAppendingPathComponent:@"Exception.txt"]; [url writeToFile:path atomically:YES encoding:NSUTF8StringEncodingerror:nil]; //除了可以選擇寫到應用下的某個檔案,通過後續處理將資訊發送到伺服器等 //還可以選擇調用發送郵件的的程式,發送資訊到指定的郵件地址 //或者調用某個處理常式來處理這個資訊 } @implementation NdUncaughtExceptionHandler -(NSString *)applicationDocumentsDirectory { return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject]; } + (void)setDefaultHandler { NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler); } + (NSUncaughtExceptionHandler*)getHandler { return NSGetUncaughtExceptionHandler(); } @end |