You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native 0.71替换AppDelegate.mm为AppDelegate.swift启动报错求助

解决React Native 0.71替换AppDelegate为Swift后启动报错问题

问题原因

你的Swift版AppDelegate存在三个关键错误,导致moduleName未被正确传递给父类,触发"A moduleName is required to create an RCTRootView"错误:

  1. application方法签名错误:未添加override关键字,且参数格式不符合iOS原生方法规范,系统实际调用的是父类RCTAppDelegate的默认实现,此时moduleName还未被赋值。
  2. 递归调用死循环:方法内调用self.application(application, didFinishLaunchingWithOptions: launchOptions)会无限递归,阻断正常启动流程。
  3. concurrentRootEnabled未重写:未添加override关键字,无法覆盖父类方法,可能影响并发根视图的启用逻辑。

修正后的AppDelegate.swift代码

import Foundation
import UIKit

@UIApplicationMain
class AppDelegate: RCTAppDelegate {

  override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
      // 保留你的自定义逻辑,比如主题设置
      // CommonTheme.currentTheme.primaryColor = .red;
      self.moduleName = "RN0710RC3"
      return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

  override func sourceURL(for bridge: RCTBridge!) -> URL! {
      #if DEBUG
      return RCTBundleURLProvider.sharedSettings()?.jsBundleURL(forBundleRoot: "index", fallbackResource: nil)
      #else
      return Bundle.main.url(forResource: "main", withExtension: "jsbundle")
  }

  override func concurrentRootEnabled() -> Bool {
      return true
  }
}

额外检查项

  1. 桥接文件配置:确保Xcode项目的"Build Settings"中,"Objective-C Bridging Header"路径正确指向你的Bridging-Header.h文件(格式通常为$(SRCROOT)/项目名/Bridging-Header.h)。
  2. 删除冗余文件:确认已彻底删除原有的AppDelegate.mm、AppDelegate.h、main.m文件,避免Xcode编译冲突。
  3. 清理编译缓存:执行Cmd+Shift+K清理Xcode缓存,然后重新编译运行项目。

内容的提问来源于stack exchange,提问作者Ryan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 11:01:01