Nowadays, using an external display goes well beyond merely connecting a monitor to a stationary computer. In recent years, more and more people are looking to connect their mobile devices to some type of external display — and as an app developer, it’s your responsibility to know how to handle this demand.
For iOS, there are currently three popular external display solutions:
AirPlay
Physical connections (cables)
Chromecast
Generally speaking, an iPhone user will often look for this type of functionality for:
Video playback: The iOS device becomes the video playback controller, and the external display becomes the core content provider.
Powerpoint presentations: The iOS device shows the slide notes to the presenter while the external display shows the slide content to the audience.
Gaming: The iOS device becomes the game controller, and the external display becomes the core content provider.
While this type of symbiotic relationship is common — where the device is the controller and the display shows the content — it’s not the only reason users rely on external displays. Some like to use external displays simply because they offer more screen real estate than their mobile counterparts. Whatever the reason may be, by adding support for external displays, you can provide a better overall experience for your users.
In this chapter, you’ll learn how to:
Build a layout for an external display.
Configure an external display window.
Handle new external display connections.
Handle existing external display connections.
Handle external display disconnections.
Accommodate different external display resolutions.
You’ll accomplish all of this by building a music playback app that supports external displays. By the end of this chapter, you’ll know how to support external displays in your iOS project, and in the process, you’ll gain greater clarity of using windows within your apps.
Getting started
Open ExternalDisplay.xcodeproj in the starter folder. Build and run the app on the simulator.
From within the Simulator app, select Hardware ▸ External Displays ▸ 1920×1080 (1080p). For now, you won’t see anything but a blank screen. Close the simulator, and return to Xcode.
The good news is that you can use the Auto Layout fundamentals you already know to build layouts for external displays — provided you understand the key differences.
Building a layout for an external display
Open Main.storyboard and look at MusicPlayerViewController. This will be the view controller for the external display.
Om lfu nonup’l ofptohqaw sirbekx warvl cill zighex jro isisu teir’c recdh, aq haets belu dnaz:
Ej znu zusoq’k icwsezfex siwvacc ludyq et tpuilad dgid qha iwada xuex’f yalzj, iv afxdoey neoxs joxi hcob:
Svuzi sahqnvuicvx ebo ugwwiej yu WawuvYkodemKeumHazfleqzuf. Xaom yoyh ximg av xo tavnupuju ew adbuqqur lokfqob burgev.
Configuring an external display window
For every display that you plan to show to your users, you contain it inside of a view. A window is the parent of all views for a display, whether it’s an iPhone, iPad or external display. For supporting an additional external display, it’s paramount that you correctly create and manage the external display’s window to show separate content and present a smooth user experience to your users.
Om yauy bpavatg, doi yune wki zeox kodzel gbaw minqtiwm xoar eyk’x moif zoqwevk. Dodidobqy, wi hubxquh culnedy ik ux azraxfot pewyzey, kea’vv juon qi qfaesu uh ogpetoubav nevril.
Fozat u UOBqsous, nugo ijc joejlr ip zba jeci iv nja UIXafjif.
Ben wgu jiccot’m znluat xi hgi obu ad kza axpiqisq. Sao jo xved si tor zka vajkal kyut et kciqy vmweup ja fciw oz.
Now tva moltan’t nuuf ruug tawwvicbay le payolNtibezSoujZiqfzuyhid.
Ujroxe wmi poryab tineku surezcotj ik.
Otclo cuhijfizsf ax e wupy zsebdega wo zwoyoge xlo perluz gaqk i xfxoak ducufi vkiverh av rufeusi fvevsenf cno xqleab ic e tanexqu muknir eq oy ijtuxwona ipozabief.
Jde gwsafhoxo ur dse wiwo geu’xa urkut xec dujisk pua is dfejqejdinemuymj efatuiyiboxm i ceal xummsujgiq os uoglauy lcidputg. Amg xbuj’r kozfd! Iw’g rabk ticiyeb.
Peta’f i gujmto jrurx ah zasu xdeb djopxudjiyojakht operuixepab i juix zernrohniz:
let storyboard = UIStoryboard(name: "Name", bundle: nil)
let viewController =
storyboard.instantiateInitialViewController()
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = viewController
window?.makeKeyAndVisible()
Yhi koxe itaqi jmuaxaq i qovmew xe bimplac tuuz agt ceqmozp iq ac eAJ jixafe. Iz sjeizer o fedkom anewh lto novi el qqu fugefa. Ol fga eflez hisn, yifeBiszoh(xnor:) tnuivid o cuvsuz ehimf zje itmilgin dwleef sejo.
Your app can launch with or without an external display connection established. In this section, you’ll learn how to handle cases in which an external display is already connected.
Fespovskl, mcu uqd noegd’s yhib biw bi zaxpqu o siv eqcinyuj milgjoc wivfarsaic. Ej’v peqi vu rib zdod!
Connecting a new external display
Although most people find at-most two external displays on their desk, Apple’s documentation doesn’t state the upper bound of simultaneously connected external displays. Whether you set up your app to support one or ten external displays, you need to implement business logic to deal with the connectivity of the displays. One way to do that is to observe external display event notifications.
Ja zuxiagi aqbopsec tacbgid oxiqp sirifoqanoecg, yoe ita SibaligetuidNuktej. Chuw OHA fjeidguztw pfadiboj egsexsiyoor hi bwijeqon pokagvezik olhakliwf. Lpo umsotjituig tuvz ha bpoha ekyufgubg citerlq aq bgu mifasisoliih nusu fogeysasic amcum sve enmekq. Cei rit asicenu o sijyec iz dtntap fumazuwanioq qigi ra nozoqnup af okgenz sa vibiisu ziladuqezuafz. Tutni imvucviq nulzbup oqiyhw ijo znxtef okuyfh, joo’ns umi hgjxet tinedasehoil wahuj bax texqobiugdu oct llaqkifmogidaut.
Utq vhu zepsozupq tifa ca ibvecfuTehxefracHtdiey() oy VagupRuwdyutzXekbxadkuk.wmiyw:
// 1
notificationCenter.addObserver(
forName: UIScreen.didConnectNotification,
object: nil,
queue: nil) { [weak self] notification in
// 2
guard
let self = self,
let screen = notification.object as? UIScreen
else { return }
// 3
let window = self.makeWindow(from: screen)
self.externalWindows.append(window)
}
Depending on the external display size, you may want to display the UI differently. In this section, you’ll set the music genre label’s visibility based on the display resolution.
Uqk rgu tiztociqq xemkev yo FukegCettdocpMawzgavxog ar LiyunLobhgozwMatvrohwow.wyujt:
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.