iOS13 external screen mirroring, getting error: setScreen should not be called

iOS13 external screen mirroring, getting error: setScreen should not be called

我试图在 iOS 13 中向外部连接的屏幕显示自定义视图控制器。我遵循了本教程:

http://www.spazstik-software.com/blog/article/how-to-display-custom-content-on-a-external-screen-from-a-ios-device

我有以下代码:

/// A private method used to setup a external screen with a window
/// loaded with a ExternalScreenViewController
///
/// - parameter screen: A UIScreen object to connect the
/// ExternalScreenViewController too
private func setupExternalScreen(screen: UIScreen) {
  guard externalWindow == nil,
    let vc = self.storyboard?.instantiateViewControllerWithIdentifier("ExternalScreen") as? ExternalScreenViewController else {
      return
  }

  externalWindow = UIWindow(frame: screen.bounds)
  externalWindow!.rootViewController = vc
  externalWindow!.screen = screen
  externalWindow!.hidden = false
}

externalWindow!.screen = screen 行中,我在 Xcode 11.4.1 控制台中收到错误消息:

[断言] UIKit 客户端错误:-如果客户端采用 UIScene 生命周期,则不应调用 [UIWindow setScreen:]。改为调用 -[UIWindow setWindowScene:]。

自定义视图控制器未显示在外部显示器中。

我应该改变什么?我想同时处理 iOS 13.x 和以前的设备。

在iOS 13 个设备中,您应该在externalWindow 对象上设置windowScene 而不是设置screen。在此之前,您应该从UIApplication的连接场景中找出匹配的windowScene。修改后的方法如下:

private func setupExternalScreen(screen: UIScreen, shouldRecurse: Bool = true) {
    // For iOS13 find matching UIWindowScene
    var matchingWindowScene: UIWindowScene? = nil
    if #available(iOS 13.0, *) {
        let scenes = UIApplication.shared.connectedScenes
        for aScene in scenes {
            if let windowScene = aScene as? UIWindowScene {
                // Look for UIWindowScene that has matching screen
                if (windowScene.screen == screen) {
                    matchingWindowScene = windowScene
                    break
                }
            }
        }
        if matchingWindowScene == nil {
            // UIWindowScene has not been created by iOS rendered yet
            // Lets recall self after delay of two seconds
            if true == shouldRecurse {
                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
                    self.setupExternalScreen(screen:screen, shouldRecurse: false)
                }
            }
            // Dont proceed furthure in iOS13
            return
        }
    }

    guard externalWindow == nil, let vc = self.storyboard?.instantiateViewControllerWithIdentifier("ExternalScreen") as? ExternalScreenViewController else {
      return
    }

    externalWindow = UIWindow(frame: screen.bounds)
    externalWindow!.rootViewController = vc
    if #available(iOS 13.0, *) {
        // Set windowScene here, no need to set screen
        externalWindow!.windowScene = matchingWindowScene
    } else {
        // Set screen the traditional way
        externalWindow!.screen = screen
    }
    externalWindow!.isHidden = false

}

注意:我们应该设置 isHidden,因为 hidden 已弃用。