---
title: "#305: Working with External Displays on iOS 📱📺"
url: https://littlebitesofcocoa.com/305-working-with-external-displays-on-ios
author: Jake Marsh
published: 2017-03-24
topics: ["UIKit"]
description: "The iPhone and iPad are awesome devices with beautiful displays. Sometimes though, we might want to show parts of our app on a bigger, external screen."
---

# #305: Working with External Displays on iOS 📱📺

By Jake Marsh, March 24, 2017. HTML: https://littlebitesofcocoa.com/305-working-with-external-displays-on-ios

The **iPhone** and **iPad** are awesome devices with beautiful _displays_. Sometimes though, we might want to show parts of our app on a bigger, <i>external</i> _screen_.

Today we'll look at how we can do just that on **iOS**, let's dive in.

Before we begin let's break down what exactly we're talking about here:

**UIKit** exposes any external _display_ we connect to our **iPhone** or **iPad** [with an adapter](http://www.apple.com/shop/product/MD826AM/A/lightning-digital-av-adapter) as a `UIScreen`. The same is also true when we turn-on [Airplay Mirroring](https://support.apple.com/en-us/HT204289) to an **Apple TV**.

Now, let's write some code.

We'll eventually use `Notifications` to learn when a _screen_ is connected/disconnected, but many users may launch our **app** with a _screen_ <i>already</i> connected, so let's start by checking for that.

We'll create a couple **instance properties** to hold our the `UIWindow` and `UIViewController` we'll be displaying externally:

``` swift
var externalDisplayWindow: UIWindow?
var externalDisplayVC: StatusBoardViewController?
```

Next, we'll make a function we can call when our **app** first launches, and when a new _display_ is connected:

``` swift
func checkForExternalDisplay() {
  guard let screen = UIScreen.screens.last
  else { return }
```

We'll create a new `UIWindow` and assign it to the external `UIScreen`:

``` swift
  if externalDisplayWindow == nil {
    externalDisplayWindow = UIWindow(
      frame: screen.bounds
    )

    externalDisplayWindow?.screen = screen
  }
```

Next, we'll need a **view controller**. We'll create one and set it as the `rootViewController` just like usual:

``` swift
  if externalDisplayVC == nil {
    externalDisplayVC = StatusBoardViewController()
    externalDisplayWindow?.rootViewController = externalDisplayVC
```

Last but not least, we'll show the window by setting its `isHidden` property to `false`.

``` swift
    externalDisplayWindow?.isHidden = false
  }
}
```

If we connect a _display_ now, and launch our **app**, we'll see our content displayed externally, neat!

Let's finish up by wiring up those notifications we mentioned earlier. First we'll start observing them:

``` swift
NotificationCenter.default
  .addObserver(
    forName: NSNotification.Name.UIScreenDidConnect,
    object: nil,
    queue: nil,
    using: displayConnected
  )

NotificationCenter.default
  .addObserver(
    forName: NSNotification.Name.UIScreenDidDisconnect,
    object: nil,
    queue: nil,
    using: displayDisconnected
)
```

Then we'll write those two functions to handle each notification:

``` swift
func displayConnected(notification: Notification) {
  checkForExternalDisplay()
}

func displayDisconnected(notification: Notification) {
  externalDisplayWindow?.isHidden = true

  externalDisplayVC = nil
  externalDisplayWindow = nil
}
```

Now our **app** will start/stop showing on the external display automatically when the user connects it. Very cool. 

There's plenty more to dive into when working with external displays and `UIScreen`. We'll go further in future _Bites_.

That's all for today. Have an idea or request for a _Bite_? Send it along  to [hello@littlebitesofcocoa.com](mailto:hello@littlebitesofcocoa.com)!

<p></p>
