[SwiftUI]工程最低适配iOS13

问题:

新建工程,选择最低支持iOS13报错:

'main()' is only available in iOS 14.0 or newer

'Scene' is only available in iOS 14.0 or newer

'WindowGroup' is only available in iOS 14.0 or newer

解决:

注释掉上面代码,重新创建一个AppDelegate作为入口:

import Foundation
import SwiftUI

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // 创建一个UIHostingController来托管你的ContentView
        let contentView = ContentView()

        // 设置window
        let window = UIWindow(frame: UIScreen.main.bounds)
        window.backgroundColor = .white
        window.rootViewController = UIHostingController(rootView: contentView)
        self.window = window
        window.makeKeyAndVisible()
        return true
    }
}

info.plist中移除Application Scene Manifest

然后,重新运行就没问题了

注意:

如果需要支持跨平台还是乖乖最低支持iOS14

使用 @UIApplicationMain 并定义一个继承自 UIResponder 和遵循 UIApplicationDelegate 协议的 AppDelegate 类是 UIKit 应用程序的传统启动方式,它专门用于 iOS 和 iPadOS 平台。这段代码不能直接用于跨平台运行,因为它仅适用于 iOS 和 iPadOS,并且使用了 UIKit 的特定类和方法,比如 UIWindow 和 UIHostingController

猜你喜欢

转载自blog.csdn.net/u012881779/article/details/135424119