【问题标题】:Xcode 11 backward compatibility: "UIWindowScene is only available in iOS 13 or newer"Xcode 11 向后兼容性:“UIWindowScene 仅在 iOS 13 或更高版本中可用”
【发布时间】:2020-01-25 00:42:48
【问题描述】:

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

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

我该怎么办?

【问题讨论】:

  • 对于寻找支持 iOS 12 和 13 以及支持故事板或全代码用户界面的更新“单视图应用程序”Xcode 模板的任何人,请参阅github.com/rmaddy/XcodeTemplates

标签: ios xcode xcode11


【解决方案1】:

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

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

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

var window : UIWindow?

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

【讨论】:

  • 您的解决方案不错。也就是说,如果您的唯一目标是支持 iOS 12,那么简单地删除 SceneDelegate 是否有意义?当然,将var window : UIWindow? 添加回AppDelegate 仍然是必要的。要修复其他错误,您还需要从AppDelegate 中删除生成的“UISceneSession Lifecycle”方法,并从Info.plist 中删除UIApplicationSceneManifest 键/值。也许还有其他理由保留SceneDelegate
  • @JWK 您必须继续使用 UIWindowScene。这就是它在模板中的原因。
  • 我确实从 Session 224 中看到了 2020 年 4 月的要求。我认为这与制作支持分屏多任务处理的自适应应用程序有关,而不是明确要求支持多副本。我可能误会了。
  • 当然。只是想确认我可以在哪里具体了解所需场景的更多信息。我在观看 WWDC 时想到了 2020 年 4 月的要求。并不是要暗示这就是你所说的:)
  • sarunw.com/tips/create-new-ios12-project-in-xcode11 - 本教程可能对某人有所帮助。
【解决方案2】:

您能否像下面这样添加此行代码

第 1 步:-

@available 出 SceneDelegate.swift

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

//...

}

第二步:-

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

第三步:-

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

【讨论】:

  • 但这将如何与程序化实现一起工作? (没有故事板)
  • @HattoriHanzō without storyboard : 我们可以在 AppDelegate var window: UIWindow? var 根视图控制器:视图控制器!并在 didFinishLaunching 方法中添加以下行: rootViewController = ViewController() self.window = UIWindow.init() self.window?.bounds = UIScreen.main.bounds self.window?.rootViewController = rootViewController self.window?.backgroundColor = 。白色 self.window?.makeKeyAndVisible()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-07
  • 1970-01-01
  • 1970-01-01
  • 2021-10-18
  • 2020-02-09
相关资源
最近更新 更多