Xcode 11向后兼容:“ UIWindowScene仅在iOS 13或更高版本中可用”

时间:2019-07-06 19:12:17

标签: ios xcode xcode11

在Xcode 11中,我从Single View App模板创建了一个新的应用程序项目。我希望该应用程序可以在iOS 12和iOS 13中运行。但是,当我将部署目标切换到iOS 12时,我收到很多错误消息,说明“ UIWindowScene仅在iOS 13或更高版本中可用”。我该怎么办?

2 个答案:

答案 0 :(得分:35)

请您添加如下所示的行代码

STEP1:-

@可使用SceneDelegate.swift

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

//...

}

STEP2:-

@@ 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:-

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

答案 1 :(得分:1)

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-而您的其他然后,代码可能需要考虑那个以便向后兼容。