iOS 捕捉异常 写文件

一. 第三方

bugtags.com


二. 系统方法

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


转载于:https://my.oschina.net/dkdsj/blog/608052

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值