iOS-APP崩溃,APP不退出应用怎么实现

简介

当APP遇到崩溃时,APP会闪退,那要怎么实现不让他闪退,比如弹出一个提示框
1231665759176_.pic.jpg

这就要用到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];
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容