React Native构建iOS时出现Application tried to present modally view controller错误
报错触发原因
- 该报错是iOS系统的原生视图跳转规则校验不通过导致:iOS不允许重复模态弹出同一个视图控制器,当代码尝试对已经处于展示状态的视图控制器再次执行present操作时,就会触发该崩溃。
- 结合你提供的AppDelegate代码,该问题多出现于生产包构建场景:
EXUpdatesAppController的startAndShowLaunchScreen:方法已经完成了启动屏视图控制器的挂载和弹出逻辑,而后续执行的[super application:application didFinishLaunchingWithOptions:launchOptions]调用又重复触发了根视图的弹出逻辑,两者冲突导致崩溃。
修复方案
方案1:调整父类方法调用顺序(优先级最高)
Expo Updates的官方集成规则要求,父类的didFinishLaunchingWithOptions方法需要放在逻辑最开头执行,避免后续视图挂载逻辑重复执行,修改后的代码如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 优先调用父类方法,获取返回值后续直接返回 BOOL superResult = [super application:application didFinishLaunchingWithOptions:launchOptions]; [FIRApp configure]; #if defined(FB_SONARKIT_ENABLED) && __has_include(<FlipperKit/FlipperClient.h>) InitializeFlipper(application); #endif self.moduleRegistryAdapter = [[UMModuleRegistryAdapter alloc] initWithModuleRegistryProvider:[[UMModuleRegistryProvider alloc] init]]; self.launchOptions = launchOptions; self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; #ifdef DEBUG [self initializeReactNativeApp]; #else EXUpdatesAppController *controller = [EXUpdatesAppController sharedInstance]; controller.delegate = self; [controller startAndShowLaunchScreen:self.window]; #endif return superResult; }
方案2:补充视图控制器弹出的校验逻辑
如果调整顺序后问题仍然存在,可以在所有执行present操作的原生代码位置,增加状态判断,避免重复弹出:
// 只有当目标控制器没有被弹出、且当前根控制器没有正在展示的弹出层时,才执行present操作 if (targetVc.presentingViewController == nil && !self.window.rootViewController.presentedViewController) { [self.window.rootViewController presentViewController:targetVc animated:YES completion:nil]; }
方案3:检查EXUpdates代理实现
确保你正确实现了EXUpdatesAppControllerDelegate的appControllerDidFinishLaunching:代理方法,React Native根视图的初始化逻辑需要放在该代理方法内执行,不要在didFinishLaunchingWithOptions中重复设置根视图:
- (void)appControllerDidFinishLaunching:(EXUpdatesAppController *)appController { [self initializeReactNativeApp]; }
内容的提问来源于stack exchange,提问作者pradip vavhal
相关产品推荐
相关产品推荐

