删除SceneDelegate后在AppDelagate中设置rootviewcontoller

时间:2020-01-09 15:16:19

标签: ios swift swiftui appdelegate

我删除了SceneDelegate.swift并从.plist文件中删除了相关条目。

由于我的项目不支持iPad,因此不需要任何SceneDelegate东西。我说的对吗?

我使用SwiftUI,所以也没有Storyboard

现在在AppDelegate文件中

var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        self.window = UIWindow(frame: UIScreen.main.bounds)

  if (TegKeychain.get("ISLOGGEDIN") == "1") {

           //present WelcomeScreen.swift

        } else {

            //present Login.Swift
        }

        return true
    }

其他文件

struct Login: View {
    var body: some View {
        Text("Login")
    }
}

struct WelcomeScreen: View {
    var body: some View {
        Text("WelcomeScreen")
    }
}

我在网上搜索,但找不到关于如何在没有故事板的情况下呈现视图控制器的任何有用信息。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

以下是我为从模板创建的新SwiftUI iOS项目回滚场景支持所做的检查清单:

  1. 从Info.plist中删除UIApplicationSceneManifest密钥

  2. 从AppDelegate中删除所有与场景会话相关的委托方法

  3. 删除SceneDelegate

  4. 更新的AppDelegate如下

    import UIKit
    import SwiftUI

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {

        var window: UIWindow?

        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            let contentView = ContentView() // << default Hello World

            // Use a UIHostingController as window root view controller.
            let window = UIWindow(frame: UIScreen.main.bounds)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()

            return true
        }

    }
  1. 构建并运行-有效。