iOS 捕捉异常 写文件

1. 代码

//AppDelegate.m

NSUncaughtExceptionHandler* _uncaughtExceptionHandler = nil;
void UncaughtExceptionHandler1(NSException *exception) {
//    NSLog(@"CRASH: %@", exception);
//    NSLog(@"Stack Trace: %@", [exception callStackSymbols]);
    
    // 异常的堆栈信息
    NSArray *stackArray = [exception callStackSymbols];
    // 出现异常的原因
    NSString *reason = [exception reason];
    // 异常名称
    NSString *name = [exception name];
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"CrashLog.plist"];
    NSLog(@"path:%@", path);
    
    NSString *logToFile = [NSString stringWithFormat:@"Stack Trace: %@", [exception callStackSymbols]];
    NSMutableDictionary *dic = @{@"logKey":logToFile};
    if ([dic writeToFile:path atomically:YES]) {
        NSLog(@"write to crash log");
    } else {
        NSLog(@"failed write to crash log");
    }
    
    return;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 保存系统处理异常的Handler
    _uncaughtExceptionHandler = NSGetUncaughtExceptionHandler();
    
    // 设置处理异常的Handler
    NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler1);
    
    //触发异常
    NSArray *arrayT = @[@"1"];
    NSLog(@"arrarT[2]:%@", arrayT[2]);
}

2. 打印

2016-01-22 14:50:34.050 pmb[2385:1103350] path:/Users/jinchuiioskaifa_1/Library/Developer/CoreSimulator/Devices/60A7787D-8D6F-4A84-AE7A-188483D8BD12/data/Containers/Data/Application/E226712D-2BDA-4135-97D4-E3C9FDE29C50/Documents/CrashLog.plist
2016-01-22 14:50:34.053 pmb[2385:1103350] write to crash log
2016-01-22 14:50:34.053 pmb[2385:1103350] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 0]'
*** First throw call stack:
(
	0   CoreFoundation                      0x000000010e0dbe65 __exceptionPreprocess + 165,
	1   libobjc.A.dylib                     0x000000010da86deb objc_exception_throw + 48,
	2   CoreFoundation                      0x000000010dfca534 -[__NSArrayI objectAtIndex:] + 164,
	3   pmb                                 0x0000000109389fbe -[AppDelegate application:didFinishLaunchingWithOptions:] + 206,
	4   UIKit                               0x000000010c0ea1f1 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 272,
	5   UIKit                               0x000000010c0eb397 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 3415,
	6   UIKit                               0x000000010c0f1cc6 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1760,
	7   UIKit                               0x000000010c0eee7b -[UIApplication workspaceDidEndTransaction:] + 188,
	8   FrontBoardServices                  0x0000000110546754 -[FBSSerialQueue _performNext] + 192,
	9   FrontBoardServices                  0x0000000110546ac2 -[FBSSerialQueue _performNextFromRunLoopSource] + 45,
	10  CoreFoundation                      0x000000010e007a31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17,
	11  CoreFoundation                      0x000000010dffd95c __CFRunLoopDoSources0 + 556,
	12  CoreFoundation                      0x000000010dffce13 __CFRunLoopRun + 867,
	13  CoreFoundation                      0x000000010dffc828 CFRunLoopRunSpecific + 488,
	14  UIKit                               0x000000010c0ee7cd -[UIApplication _run] + 402,
	15  UIKit                               0x000000010c0f3610 UIApplicationMain + 171,
	16  pmb                                 0x00000001094371ef main + 111,
	17  libdyld.dylib                       0x000000010e9fa92d start + 1,
)
libc++abi.dylib: terminating with uncaught exception of type NSException


你可能感兴趣的:(ios,Crash)