连接场景未出现在`UIApplication.shared.connectedScenes`中

问题描述

我正在尝试向我的应用程序添加外部显示支持。该应用程序最初是使用 AppDelegate 构建的,但我已将其迁移到使用 SceneDelegate,因为有人告诉我它是 iOS 13 后外部显示支持所必需的。我更新了我的 {{1 }},有一个用于主应用程序的 info.plist 和一个用于外部显示器的 SceneDelegate,我在几个不同的教程中看到过。两个代表以及 plist 更改都发布在下面。

Info.plist 添加

ExtSceneDelegate

SceneDelegate.swift

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

ExtSceneDelegate.swift

import Foundation

class SceneDelegate: UIResponder,UIWindowSceneDelegate {
    var window: UIWindow?
    
    func scene(_ scene: UIScene,willConnectTo session: UISceneSession,options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScene = scene as? UIWindowScene else { return }
        let window = UIWindow(windowScene: windowScene)
        
        // Provide your apps root view controller
        let initialViewController = RaiseBookViewController()
        window.rootViewController = initialViewController
        self.window = window
        window.makeKeyAndVisible()
    }
}

在我的应用程序中,我想根据用户的活动更新外部显示。为此,我注册了这样的通知:

import Foundation

class ExtSceneDelegate: UIResponder,options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScene = scene as? UIWindowScene else { return }
        let window = UIWindow(windowScene: windowScene)
        
        // Provide your apps root view controller
        let hostingController = HostingControllerPresentable(rootView: DefaultPresentationView(appIcon: Bundle.main.icon ?? UIImage()))
        window.rootViewController = hostingController
        self.window = window
        window.makeKeyAndVisible()
    }
}

然后尝试设置连接的第二个显示器。

func registerForScreenNotifications() {
        let notificationCentre = NotificationCenter.default
        notificationCentre.addObserver(self,selector: #selector(self.externalScreenDidConnect(notification:)),name: UIScreen.didConnectNotification,object: nil)
    }

@objc func externalScreenDidConnect(notification: NSNotification) {
        guard let screen = notification.object as? UIScreen else {
            return
        }
        
        setupScreen(screen: screen,shouldRecurse: false)
    }

按原样运行代码时,由于 @objc func setupScreen(screen: UIScreen,shouldRecurse: Bool) { // For iOS13 find matching UIWindowScene var matchingWindowScene: UIWindowScene? 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.setupScreen(screen: screen,shouldRecurse: false) } } // Dont proceed furthure in iOS13 return } } let vc = HostingControllerPresentable(rootView: DefaultPresentationView(appIcon: Bundle.main.icon ?? UIImage())) var 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 } ,外部显示发生了变化。但是,如果我尝试更改外部显示器上显示的内容,ExtSceneDelegate 函数找不到任何匹配的显示器。如果我在 setupScreen 处设置断点,则仅返回一个元素,即使 事实上 连接了两个显示器。我终其一生都无法弄清楚这一点。并希望得到任何帮助。

注意:我知道在这段代码中,我试图在 let scenes = UIApplication.shared.connectedScenesExtSceneDelegate 函数中为外部显示器设置相同的视图。我知道如果它在工作,我不会注意到任何可见的变化,但我知道代码没有被执行,即使我尝试将它设置为不同的视图,也没有任何反应。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...