繁体   English   中英

Xcode 11 向后兼容性:“UIWindowScene 仅适用于 iOS 13 或更高版本”

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

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

UIWindowScene 仅适用于 iOS 13 或更高版本

我该怎么办?

Xcode 11 中的模板使用场景委托。 场景委托和相关类是 iOS 13 中的新功能; 它们在 iOS 12 及之前不存在,并且启动过程不同。

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

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

var window : UIWindow?

结果是当这个应用程序在 iOS 13 中运行时,场景委托有window ,但是当它在 iOS 12 或更早版本中运行时,应用程序委托有window - 然后您的其他代码可能需要考虑到这一点,以便向后兼容。

您能否添加如下所示的行代码

第1步:-

@available 出 SceneDelegate.swift

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

//...

}

第2步:-

@available 在 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
    }

亚光答案可以帮助我减少错误的数量,但是我仍然必须在ContentView.swift添加一些其他@available(iOS 13.0, *)来修复所有错误。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM