Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 553
jumpwire Avatar asked Sep 11 '25 21:09

jumpwire


2 Answers

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

like image 147
jumpwire Avatar answered Sep 14 '25 11:09

jumpwire


Mirrored screens are not represented in screens Array. Instead use main screens mirroredScreen property

UPDATE:

let mirrored = UIScreen.main.mirrored
self.initializeExternal(external: mirrored)
like image 37
Максуд Даудов Avatar answered Sep 14 '25 11:09

Максуд Даудов