崩溃
来源:互联网 发布:python装饰器原理 编辑:程序博客网 时间:2024/04/29 08:23
异常处理
- 抛出异常。此时程序会强制停止
@throw [NSException exceptionWithName:@"牛逼的错误" reason:@"方法找不到" userInfo:nil];
[NSException raise:@"牛逼的错误" format:@"%s",__func__];
- 拦截异常
@try { @[][0]; } @catch (NSException *exception) { NSLog(@"代码有异常-%@",exception); } @finally { NSLog(@"finally"); }
- 如果try里面的代码有错误,会执行catch,然后执行finally;如果try没有错误,执行完try,直接执行finally
崩溃统计分析
- 方法一。在main函数中拦截崩溃。不过一般不这么干。
int main(int argc, char * argv[]) { @try { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } } @catch (NSException *exception) { //存放崩溃信息到本地 NSString *file = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)objectAtIndex:0]; NSString *fileName = [file stringByAppendingPathComponent:@"exception"]; [[exception callStackSymbols] writeToFile:fileName atomically:YES]; } @finally { }}
- 方法二。在appDelegate中拦截异常。在崩溃之前拦截。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //这句是关键代码 NSSetUncaughtExceptionHandler(handleException); return YES;}//函数名随意void handleException(NSException *exception){//崩溃信息文件存放位置 NSString *file = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)objectAtIndex:0]; NSLog(@"file--%@",file); NSString *fileName = [file stringByAppendingPathComponent:@"exception"]; //崩溃信息字典 NSMutableDictionary *info = [NSMutableDictionary dictionary]; info[@"callStack"] = [exception callStackSymbols];//调用栈信息(错误来源于哪个方法) info[@"name"] = [exception name];//异常名字 info[@"reason"] = [exception reason]; //异常描述(报错理由) [info writeToFile:fileName atomically:YES];}
- 方法三。第三方
- 友盟
- Flurry
- Crashlytics
弹出提醒的方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSSetUncaughtExceptionHandler(handleException); return YES;}void handleException(NSException *exception){ [[UIApplication sharedApplication].delegate performSelector:@selector(handle)];}-(void)handle{ UIAlertView *view = [[UIAlertView alloc]initWithTitle:@"哈哈" message:@"我是闪退前的挣扎" delegate:self cancelButtonTitle:@"好的" otherButtonTitles: nil]; [view show]; //重新启动runloop。这句是关键代码 [[NSRunLoop currentRunLoop]addPort:[NSPort port] forMode:NSDefaultRunLoopMode]; [[NSRunLoop currentRunLoop]run];}-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSLog(@"点击了好的"); exit(0);}
阅读全文
0 0
- 崩溃
- 崩溃
- 崩溃
- 崩溃
- 崩溃
- 崩溃
- 崩溃
- 崩溃
- 崩溃
- 崩溃,崩溃
- 全线崩溃
- cvsnt崩溃
- VNCServer崩溃
- 华尔街崩溃
- 硬盘崩溃
- Firefox崩溃
- 输入法崩溃
- 崩溃恢复
- Android:错误记录,便于查询
- 每天学习8小时以下是不道德的
- 高速公路ETC卡签之我见4-卡签结构说明
- 在scanf()中使用空格来适应更多形式的输入
- android studio 解决内存泄漏
- 崩溃
- java对象和xml的互相转换
- 在ARM-linux上实现4G模块PPP拨号上网
- 立即表达式的多种写法与注意点以及in操作符的作用
- 函数成了一等公民
- Android中倒计时库CountdownView倒计时商品秒杀
- Retrofit 2.0 使用教程
- 操作系统中锁的实现原理
- spring cloud eureka高可用踩的坑