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

mat*_*att 22 xcode ios xcode11

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

UIWindowScene仅在iOS 13或更高版本中可用

我该怎么办?

IKK*_*KKA 50

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

第1步:-

@available 出 SceneDelegate.swift

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

//...

}
Run Code Online (Sandbox Code Playgroud)

第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.
}
Run Code Online (Sandbox Code Playgroud)

第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
    }
Run Code Online (Sandbox Code Playgroud)


mat*_*att 44

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

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

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

var window : UIWindow?
Run Code Online (Sandbox Code Playgroud)

结果是,当此应用程序在iOS 13中运行时,场景委托具有window,但在iOS 12或更低版本中运行时,该应用程序委托具有window-,然后您的其他代码可能需要考虑该因素,以便向下兼容。

  • https://sarunw.com/tips/create-new-ios12-project-in-xcode11/ - 本教程可能会对某人有所帮助。 (11认同)
  • 您的解决方案是一个很好的解决方案。也就是说,如果您唯一的目标是支持iOS 12,那么简单地删除`SceneDelegate`是否有意义?当然仍然需要将“ var window:UIWindow?”添加回“ AppDelegate”。要解决其他错误,您还需要从“ AppDelegate”中删除生成的“ UISceneSession Lifecycle”方法,并从“ Info.plist”中删除“ UIApplicationSceneManifest”键/值。也许还有其他原因保留`SceneDelegate`吗? (3认同)
  • 当然。只是尝试确认具体可以在哪里了解有关所需场景的更多信息。我在WWDC的观察中想到了2020年4月的要求。并不是要暗示那就是你在说的:) (2认同)