[SwiftUI] The minimum project adaptation is iOS13

question:

Create a new project, select the minimum support iOS13 and report an error:

'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

solve:

Comment out the above code and re-create an AppDelegate as the entrance:

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
    }
}

Remove Application Scene Manifest from info.plist

Then, it's fine to run again

Notice:

If you need to support cross-platform or support iOS14 at least,

using  @UIApplicationMain and defining a class that inherits from  UIResponder and follows  UIApplicationDelegate the protocol  AppDelegate is the traditional way to start a UIKit application, which is specifically used for iOS and iPadOS platforms. This code is not directly intended to run cross-platform because it only works on iOS and iPadOS and uses UIKit-specific classes and methods, such as  UIWindow and  UIHostingController.

Guess you like

Origin blog.csdn.net/u012881779/article/details/135424119