在 SwiftUI 中实现外部监视器支持

ker*_*nic 6 ios swift ios13 swiftui

我对通过 Airplay 和 SwiftUI 实现外部显示器支持感到困惑。

在 SceneDelegate.swift 中,我正在使用UIScreen.didConnectNotification观察者,它实际上检测到一个新的屏幕被附加,但我无法将自定义 UIScene 分配给屏幕。

我发现了一些在 iOS12 及更低版本中使用 Swift 的好例子,但它们都不适用于 SwiftUI,因为整个范式已更改为使用 UIScene 而不是 UIScreen。这是清单:

https://www.bignerdranch.com/blog/adding-external-display-support-to-your-ios-app-is-ridiculously-easy/

https://developer.apple.com/documentation/uikit/windows_and_screens/displaying_content_on_a_connected_screen

https://www.swiftjectivec.com/supporting-external-displays/

苹果去年甚至谈到了它

也许有些事情发生了变化,现在有一种新方法可以正确地做到这一点。此外,设置UIWindow.screen = screen已在 iOS13 中弃用。

有没有人已经尝试过使用 SwiftUI 实现外部屏幕支持。任何帮助深表感谢。

小智 4

我修改了 Big Nerd Ranch 博客中的示例,使其工作如下。

  1. 删除主故事板:我从新项目中删除了主故事板。在部署信息下,我将主界面设置为空字符串。

  2. 编辑 plist:在 plist 的应用程序场景清单部分定义两个场景(默认和外部)及其场景委托。

    <key>UIApplicationSceneManifest</key>
    <dict>
        <key>UIApplicationSupportsMultipleScenes</key>
        <true/>
        <key>UISceneConfigurations</key>
        <dict>
            <key>UIWindowSceneSessionRoleApplication</key>
            <array>
                <dict>
                    <key>UISceneConfigurationName</key>
                    <string>Default Configuration</string>
                    <key>UISceneDelegateClassName</key>
                    <string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
                </dict>
            </array>
            <key>UIWindowSceneSessionRoleExternalDisplay</key>
            <array>
                <dict>
                    <key>UISceneDelegateClassName</key>
                    <string>$(PRODUCT_MODULE_NAME).ExtSceneDelegate</string>
                    <key>UISceneConfigurationName</key>
                    <string>External Configuration</string>
                </dict>
            </array>
        </dict>
    </dict>
Run Code Online (Sandbox Code Playgroud)
  1. 编辑视图控制器以显示一个简单的字符串:
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .blue
        view.addSubview(screenLabel)
    }

    var screenLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.white
        label.font = UIFont(name: "Helvetica-Bold", size: 22)
        return label
    }()

    override func viewDidLayoutSubviews() {
        /* Set the frame when the layout is changed */
        screenLabel.frame = CGRect(x: 0,
                                y: 0,
                                width: view.frame.width - 30,
                                height: 24)
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. 修改 SceneDelegate 中的 scene(_:willConnectTo:options:) 以在主 (iPad) 窗口中显示信息。
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let windowScene = (scene as? UIWindowScene) else { return }

        window = UIWindow(frame: windowScene.coordinateSpace.bounds)
        window?.windowScene = windowScene
        let vc = ViewController()
        vc.loadViewIfNeeded()
        vc.screenLabel.text = String(describing: window)
        window?.rootViewController = vc
        window?.makeKeyAndVisible()
        window?.isHidden = false
    }
Run Code Online (Sandbox Code Playgroud)
  1. 为您的外部屏幕创建一个场景委托。我创建了一个新的 Swift 文件 ExtSceneDelegate.swift,其中包含与 SceneDelegate.swift 相同的文本,并将类的名称从 SceneDelegate 更改为 ExtSceneDelegate。

  2. 修改 AppDelegate 中的 application(_:configurationForConnecting:options:) 。其他人建议,如果您将其注释掉,一切都会好起来的。为了调试,我发现将其更改为以下内容很有帮助:

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {

        // This is not necessary; however, I found it useful for debugging
        switch connectingSceneSession.role.rawValue {
            case "UIWindowSceneSessionRoleApplication":
                return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
            case "UIWindowSceneSessionRoleExternalDisplay":
                return UISceneConfiguration(name: "External Configuration", sessionRole: connectingSceneSession.role)
            default:
                fatalError("Unknown Configuration \(connectingSceneSession.role.rawValue)")
            }
    }
Run Code Online (Sandbox Code Playgroud)
  1. 在 iOS 上构建并运行应用程序。您应该看到一个丑陋的蓝屏,其中包含有关 UIWindow 的信息。然后我使用屏幕镜像连接到 Apple TV。您应该在外部屏幕上看到类似丑陋的蓝屏,并具有不同的 UIWindow 信息。

对我来说,解决所有这些问题的关键参考是https://onmyway133.github.io/blog/How-to-use-external-display-in-iOS/