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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/56916948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:59:40  来源:igfitidea点击:

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

iosxcodexcode11

提问by matt

In Xcode 11, I created a new app project from the Single View App template. I want this app to run in iOS 12 as well as iOS 13. But when I switch the deployment target to iOS 12, I got a lot of error messages like this one:

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

UIWindowScene is only available in iOS 13 or newer

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

What should I do?

我该怎么办?

回答by matt

The template in Xcode 11 uses a scene delegate. Scene delegates and the related classes are new in iOS 13; they don't exist in iOS 12 and before, and the launch process is different.

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

To make a project generated from an Xcode 11 app template backward compatible, you need to mark the entire SceneDelegate class, and any methods in the AppDelegate class that refer to UISceneSession, as @available(iOS 13.0, *).

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

You also need to declare a windowproperty in the AppDelegate class (if you don't do that, the app will run and launch but the screen will be black):

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

var window : UIWindow?

The result is that when this app runs in iOS 13, the scene delegate has the window, but when it runs in iOS 12 or before, the app delegate has the window— and your other code may then need to take account of thatin order to be backward compatible.

其结果是,当在iOS中13这个程序运行时,现场代表有window,但是当它在iOS的12运行或之前,应用程序委托有window-然后你的其他代码可能需要考虑的在为了向下兼容。

回答by IKKA

Could you please add this line code like the following

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

STEP1:-

第1步:-

@available out the SceneDelegate.swift

@available 出 SceneDelegate.swift

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

//...

}

STEP2:-

第2步:-

@available out some methods in AppDelegate.swift

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

STEP3:-

第3步:-

You should declare windowproperty in AppDelegate.swift file like var window: UIWindow?

您应该在 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
    }