React Native 0.85 在 Xcode 27 启动失败:需 UIScene 生命周期
结论
这是 Xcode 27 所用 iOS SDK 对 UIScene 生命周期的强制检查。JS 层和 New Architecture 本身没有问题。RN 0.85 默认模板仍通过 UIApplicationDelegate 创建 UIWindow,链接新 SDK 后可以编译,但启动时会被 UIKit 拦下。处理方式是在 iOS 原生层补上 UIApplicationSceneManifest,再由 UISceneDelegate 接管窗口和根视图的创建。
原因
UIScene life cycle is required for apps built with this SDK 这条报错说明,应用链接了要求 Scene 生命周期的 SDK。UIApplicationSceneManifest 必须配置;只加清单,却没有把 RN 根视图挂到 scene 的 UIWindow 上,启动后通常会出现黑屏或白屏。New Architecture 仍沿用同一套 UIKit 启动流程,因此迁移点还是在 AppDelegate 和 SceneDelegate。
迁移步骤
- 在 iOS 工程的
Info.plist中加入 Scene Manifest。单窗口应用将UIApplicationSupportsMultipleScenes设为false。
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<false/>
<key>UISceneConfigurations</key>
<dict>
<key>UIWindowSceneSessionRoleApplication</key>
<array>
<dict>
<key>UISceneConfigurationName</key>
<string>Default Configuration</string>
<key>UISceneDelegateClassName</key>
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
</dict>
</array>
</dict>
</dict>
- 新建
SceneDelegate.swift并加入 iOS target。这里要让SceneDelegate创建UIWindow(windowScene:),根视图控制器仍由 RN 的工厂创建。下面的示例按 RN 0.85 的RCTAppDelegate暴露reactNativeFactory、moduleName、initialProps的常见结构编写;如果你的模板方法名不同,以本地头文件为准。
import UIKit
import React
import React_RCTAppDelegate
final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene,
let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let rootVC = appDelegate.reactNativeFactory.rootViewController(
withModuleName: appDelegate.moduleName,
initialProperties: appDelegate.initialProps,
launchOptions: nil
)
let window = UIWindow(windowScene: windowScene)
window.rootViewController = rootVC
self.window = window
window.makeKeyAndVisible()
}
}
-
AppDelegate保留 RN 模板中的moduleName、dependencyProvider、initialProps和bundleURL配置。当前模板的super.application(_:didFinishLaunchingWithOptions:)如果仍会创建window,迁移时不要把它当作 scene 的窗口使用,应以SceneDelegate中的 window 为准。若出现重复窗口或根视图空白,删除AppDelegate里创建 window 和调用makeKeyAndVisible()的逻辑,只保留 RN 工厂初始化。 -
清理后重新安装。修改
Info.plist后要重新构建并安装。模拟器上可以删除旧 App 或执行 Erase Simulator,真机上删除旧 App 后再安装,避免旧的Info.plist被缓存。
注意事项
- 不要只添加
UIApplicationSceneManifest就结束,必须让SceneDelegate设置window.rootViewController。 - 项目若自定义了
RCTRootView,或用RCTReactNativeFactory手动创建根视图,可以把这段逻辑抽成makeRootViewController(),再在SceneDelegate中调用。 - 使用 Swift 时,
UISceneDelegateClassName通常写成$(PRODUCT_MODULE_NAME).SceneDelegate;如果使用 Objective-C,类名要按 target 模块配置调整。 - 如果后续 RN 版本已经内置 UIScene 模板,优先按官方升级说明迁移,减少长期维护自定义
SceneDelegate的成本。 - 以上迁移不会改动 JS 代码,也不需要关闭 New Architecture。
备注:内容仅供参考。
