App闪退一直是大家头疼的问题,尤其是未知的没有办法很好的做容错处理,今天就结合runloop处理崩溃问题
效果实现:
用法:
1.将截图的中CatchedHelper
文件夹拖到你的项目工程中。
2.在AppDelegate.m
中找到以下方法并如下添加代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[UncaughtExceptionHandler installUncaughtExceptionHandler:YES showAlert:YES];
return YES;
}
以上代码就可以实现稍微友好一点的crash拦截处理。
代码解释:
UncaughtExceptionHandler.h
主要代码:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface UncaughtExceptionHandler : NSObject
/*!
* 异常的处理方法
*
* @param install 是否开启捕获异常
* @param showAlert 是否在发生异常时弹出alertView
*/
+ (void)installUncaughtExceptionHandler:(BOOL)install showAlert:(BOOL)showAlert;
@end
UncaughtExceptionHandler.m
文件主要的代码如下:
1.发送异常信号
/*
* 异常的处理方法
*
* @param install 是否开启捕获异常
* @param showAlert 是否在发生异常时弹出alertView
*/
+ (void)installUncaughtExceptionHandler:(BOOL)install showAlert:(BOOL)showAlert {
if (install && showAlert) {
[[self alloc] alertView:sho