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

如何在现有UIKit项目中直接启动SwiftUI主应用

实现方法

1. 调整项目Info.plist配置

找到项目中的Info.plist文件,移除Application Scene Manifest相关配置项(若存在),SwiftUI的WindowGroup会自动处理场景配置逻辑。

2. 创建SwiftUI应用入口结构体

新建一个Swift文件(例如命名为MainApp.swift),写入以下代码:

@main
struct MainAppStruct: App {
    @UIApplicationDelegateAdaptor var delegate: AppDelegate
    var body: some Scene {
        WindowGroup {
            SplashView()
        }
    }
}

若项目中已有AppDelegate类,确保它继承自UIResponder并遵循UIApplicationDelegate协议,同时删除application(_:didFinishLaunchingWithOptions:)里的窗口初始化代码。

3. 设置项目主入口

选中项目target,进入General标签页,在Deployment Info的Main Interface下拉菜单中选择刚创建的MainAppStruct(或直接留空,@main标记会自动识别为入口)。

4. 清理旧启动代码

删除SceneDelegate.swift文件(若不再需要),同时移除AppDelegate中与窗口初始化、场景配置相关的代码,比如原有的:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }
    let window = UIWindow(windowScene: windowScene)
    self.window = window
    let splashVCHostingController = UIHostingController(rootView: SplashView())
    self.window.rootViewController = splashVCHostingController
    window.makeKeyAndVisible()
}

完成以上步骤后,项目就会通过@main标记的SwiftUI App结构体直接启动,自动加载SplashView作为根视图,同时保留原有AppDelegate的业务逻辑。

内容的提问来源于stack exchange,提问作者Aaban Tariq Murtaza

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 14:20:56