Xcode 11 向后兼容性:"UIWindowScene is only available in iOS 13 or newer"

Xcode 11 backward compatibility: "UIWindowScene is only available in iOS 13 or newer"

在 Xcode11 中,我从 Single View App 模板创建了一个新的应用程序项目。我希望此应用程序在 iOS 12 和 iOS 13 中 运行。但是当我将部署目标切换到 iOS 12 时,我收到了很多错误消息,例如这个:

UIWindowScene is only available in iOS 13 or newer

我该怎么办?

Xcode11 中的模板使用了场景委托。场景委托和相关的 classes 是 iOS 13 中的新内容; iOS 12 及之前不存在,启动过程不同。

要使从 Xcode 11 应用程序模板生成的项目向后兼容,您需要标记整个 SceneDelegate class 以及 AppDelegate class 中引用的任何方法UISceneSession,如 @available(iOS 13.0, *).

您还需要在 AppDelegate class 中声明 window 属性(如果您不这样做,应用程序将 运行 并启动,但屏幕将变黑):

var window : UIWindow?

结果是当这个应用程序 运行s 在 iOS 13 时,场景委托有 window,但是当它 运行s 在 [=25] =] 12 或之前,app delegate 有 window — 然后你的其他代码可能需要考虑 that 以便向后兼容。

你能不能像下面这样添加这行代码

第一步:-

@可用 SceneDelegate.swift

@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {

//...

}

第二步:-

@可用AppDelegate.swift

中的一些方法
// AppDelegate.swift

@available(iOS 13.0, *)
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    // Called when a new scene session is being created.
    // Use this method to select a configuration to create the new scene with.
    return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

@available(iOS 13.0, *)
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
    // Called when the user discards a scene session.
    // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
    // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}

第 3 步:-

您应该在 AppDelegate.swift 文件中声明 window 属性 像 var window: UIWindow?

class AppDelegate: UIResponder, UIApplicationDelegate {

     var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }