简介
当APP遇到崩溃时,APP会闪退,那要怎么实现不让他闪退,比如弹出一个提示框
这就要用到runloop技术了
强行让runloop继续运行
上代码
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self installUncaughtSignalExceptionHandler];
}
/// 获取崩溃
- (void)installUncaughtSignalExceptionHandler{
NSSetUncaughtExceptionHandler(&LGExceptionHandlers);
}
void LGExceptionHandlers(NSException *exception) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Crash" message:@"The App has crashed and will attempt to send a crash report" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *sure = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDestructive handler:nil];
[alert addAction:sure];
[[UIViewController hll_topVC] presentViewController:alert animated:YES completion:nil];
// 本次异常处理, 保证弹框能正常弹出
CFRunLoopRef runloop = CFRunLoopGetCurrent();
CFArrayRef allMode = CFRunLoopCopyAllModes(runloop);
while (YES) {
for (NSString *mode in (__bridge NSArray *)allMode) {
CFRunLoopRunInMode((CFStringRef)mode, 0.0001, false);
}
}
CFRelease(allMode);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
/// 崩溃代码
@[@""][2];
}