قالب وردپرس درنا توس
Home / IOS Development / # 305: Working with external monitors on iOS 📱📺 – Small pieces of cocoa

# 305: Working with external monitors on iOS 📱📺 – Small pieces of cocoa



Subjects

Items

iPhone and iPad are amazing devices with beautiful screens . Sometimes we may view parts of our app on a larger external screen .

Today we look at how we can do it on iOS let's go diving.

Before we begin, let's break down what we're talking about here:

UIKit reveals some external display we connect to our ] iPhone or iPad with an adapter like a UIScreen . The same also applies when we turn on the Airplay Mirroring to an Apple TV .

Now let's write some codes.

We will eventually use Notifications to learn when a screen is connected / disconnected, but many users can launch our app with a ] screen already connected, so let's start checking it. We will create a few occurrence features to hold our UIWindow and UIViewController to display remotely:

  var    :   ? ?    externalDisplayVC :    StatusBoardViewController ? 

Then we will create a feature that we can call when our app first launches and when a new screen is connected to:

  func    checkForExternalDisplay  () [1
9659027] {
guard With Window = UIScreen . displays . last others { return }

We create a new UIWindow and assign it to the external UIScreen :

    ]    externalDisplayWindow    ==   ] nil    {
      externalDisplayWindow    =    UIWindow  (
        rams :.    The [19659030]   Borders 
    ) 

      ExternalDisplayWindow ? .   screen    =    screen 
  } 

Then we need a display regulator . We will create one and set it as rootViewController just as usual:

    if    externalDisplayVC    ==    nil    {
      externalDisplayVC    =    ] StatusBoardViewController  () 
      externalDisplayWindow ? .   rootViewController    =    externalDisplayVC 

Last but not least, we display the window by setting its hidden property to false .

      externalDisplayWindow ? .   isHidden    =    false 
  } 
} [19659023] If we now connect a  screen  now and start our  app  we will see the content our externally, nicely! 

Let's finish by unplugging the messages we mentioned earlier. First, we begin to observe them:

  NotificationCenter .   standard 
  .   addObserver  
      forName :    NSNotification .] Name    UIScreenDidConnect  
      item .    nil         :    nil  
      using :    displayConnected 
  ) 

  NotificationCenter    Standard 
     addObserver  
      forName .:.    NSNotification    Name    UIScreenDidDisconnect  
      item .    nil     :    nil  
      using :    displayDisconnected 
) 

Then we will write these two features to handle every notice:

  func    displayConnected  (  notification ]:    Notice )    {
    checkForExternal Display  () 
}    FUNC    display Disconnected  (  Message :    Notice ) [19659027] {
    externalDisplayWindow   19659030].    isHidden    =    true 

    externalDisplayVC    =    nil 
    externalDisplayWindow    =    nil 
} 

Now our app start / stop automatically displaying on the external display when the user connects it. Very cool.

There is much more to dive into when working with external displays and UIScreen . We will continue in the future Bites .

It's all for today. Do you have an idea or request for a Bite ? Send it to hello@littlebitesofcocoa.com!


Source link