现有iOS项目接入SceneDelegate后白屏、scene:willConnectTo:未调用问题
iOS 12兼容项目接入SceneDelegate白屏、生命周期方法不调用修复方案
你的配置存在3个直接导致故障的问题,按以下步骤修复即可:
问题1:AppDelegate未做版本兼容隔离
兼容低版本的项目接入SceneDelegate时,必须明确告知系统iOS13+走Scene生命周期、iOS12走旧的AppDelegate生命周期,否则系统会卡在生命周期切换阶段,完全不触发SceneDelegate的回调方法。
给AppDelegate的旧生命周期方法添加版本限制,参考实现:class AppDelegate: UIResponder, UIApplicationDelegate { // iOS12及以下版本使用该window属性 var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // 仅iOS12及以下需要执行的初始化逻辑放在这里 return true } // 以下旧生命周期方法全部标记为iOS13废弃 @available(iOS, introduced: 2.0, deprecated: 13.0, message: "iOS13+ 生命周期由SceneDelegate管理") func applicationDidBecomeActive(_ application: UIApplication) { } @available(iOS, introduced: 2.0, deprecated: 13.0, message: "iOS13+ 生命周期由SceneDelegate管理") func applicationWillResignActive(_ application: UIApplication) { } @available(iOS, introduced: 2.0, deprecated: 13.0, message: "iOS13+ 生命周期由SceneDelegate管理") func applicationWillEnterForeground(_ application: UIApplication) { } @available(iOS, introduced: 2.0, deprecated: 13.0, message: "iOS13+ 生命周期由SceneDelegate管理") func applicationDidEnterBackground(_ application: UIApplication) { } }问题2:SceneDelegate未完成window初始化挂载
你当前的scene(_:willConnectTo:options:)方法仅做了场景类型转换就直接返回,没有创建window、绑定根控制器并设置为主窗口,这是启动白屏的核心原因。哪怕你在Info.plist中配置了UISceneStoryboardFile,也需要手动完成window的挂载流程,修复代码如下:@available(iOS 13.0, *) class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { guard let windowScene = scene as? UIWindowScene else { return } // 初始化绑定当前场景的window let window = UIWindow(windowScene: windowScene) // 加载你在配置中指定的Splash storyboard初始控制器 let splashStoryboard = UIStoryboard(name: "Splash", bundle: .main) guard let rootViewController = splashStoryboard.instantiateInitialViewController() else { return } window.rootViewController = rootViewController self.window = window window.makeKeyAndVisible() } // 其余生命周期方法保持原有实现即可 func sceneDidDisconnect(_ scene: UIScene) { } func sceneDidBecomeActive(_ scene: UIScene) { } func sceneWillResignActive(_ scene: UIScene) { } func sceneWillEnterForeground(_ scene: UIScene) { } func sceneDidEnterBackground(_ scene: UIScene) { } }问题3:配置缓存与资源校验
你贴出的Info.plist结构本身没有语法错误,但需要做两项校验:- 确认项目中确实存在
Splash.storyboard文件,且该文件已勾选target membership、已经设置了初始控制器(在storyboard中勾选对应控制器的Is Initial View Controller选项) - 执行Xcode清理操作:按下
Shift + Command + K清理构建产物,删除模拟器/真机上的旧安装包后重新编译,避免旧的无Scene配置的缓存包影响运行结果。
- 确认项目中确实存在
修复后验证:iOS13及以上版本系统会正常调用
scene(_:willConnectTo:options:)方法,页面正常加载无白屏;iOS12版本仍然走AppDelegate旧生命周期逻辑,兼容逻辑不受影响。
内容的提问来源于stack exchange,提问作者I Don't Tell
相关产品推荐
相关产品推荐

