iosswiftuiscreen

Detecting External Screen with iOS Swift


I am trying to detect an external display using iOS Swift. The second display is connected with a Lightning Digital AV Adapter. I have imported UIKIt. When I run the code below screens.count just gives me 1 even if I start the app with the second screen already attached and mirroring.

    override func viewDidLoad() {
    super.viewDidLoad()

    // Initialize an external screen if one is present
    let screens = UIScreen.screens
    print(screens.count)

    txtDisplay.text = String(screens.count)
    if screens.count > 1 {
        print("A second screen has been detected")
        //An external screen is available. Get the first screen available
        //self.initializeExternalScreen(externalScreen: screens[1] as UIScreen)
    }


}

I have loaded demo applications that claim to have this functionality but I just get the same result. Any ideas how I can detect a second display using Swift?

Thanks


Solution

  • I discovered that UIScreen.screens.count would only acknowledge the external second screen if I setup notifications. Once I setup observers in the NotificationCenter I finally got 'UIScreen.screens.count' to == 2. Then I was able to assign a view to UIScreen.screens1

    This page has the details: http://tutorials.tinyappco.com/Swift/AdditionalScreen