قالب وردپرس درنا توس
Home / IOS Development / Adding external screen support to your iOS app is Ridiculously Easy

Adding external screen support to your iOS app is Ridiculously Easy



Get ready for the big screen

Apple made a big splash this week with the new iPad Pro.
In the promotional videos, they have been shown to use the USB-C port to connect the iPad to an external display for creative tasks.
This is a feature that few people already know exist on all iOS devices.
You can connect an external display via a lightning adapter or AirPlay Screen Mirroring to an Apple TV.

With this small amount of code, you can listen to the connection / disconnection of screens and set up a separate window and display the remote control control hierarchy to increase the app's main content.

  import    UIKit 

  class    ViewController :    UIViewController    {
    
      // For demo purposes. We only show a string description 
      // of each UIScreen object on each display's display manager 
      @IBOutlet    was    screenLabel :    UILabel !    static    FUNC    makeFromStoryboard  ()    ->    ViewController    {
          return    UIStoryboard  (  name :    "Head"  
                             bundle .!    nil ) 
               instantiateInitialViewController  ()    som    ViewController 
    } [19659000]] 

  @UIApplicationMain [19659000]]    AppDelegate :    UIResponder     UIApplicationDelegate    {
    
      // Main window displayed on device display [19659012] // Main directory will set this automatically 
      var    window : [19659005] UIWindow ? 

      // References to our windows as we create 
      Var    windowsForScreens    =    [  UIScreen :    UIWindow ] () 

      / / Create our checks and add text to our test label 
      private    FUNC    addViewController  (  to    window :    UIWindow     text :    String ) [ ]          =    ViewController .   makeFromStoryboard  () 

          // When we need to complete the loading before opening 
          // 19659092] vc       loadViewIfNeeded  () 
          ] vc .   screen label .  ] ]  ]  ]    rootViewController    =    vc 
    } 

      // Create and set up a new window with our display controls like the root 
      private    FUNC    setupWindow  (  to    screen :    UIScreen )    {
          la    window    ]    UIWindow  () 
          addViewController  (  to :    window     text :    String    describes :    screen [19659008])) [19659092vinduet] .   the window    =    screen [19659092vinduet] .   
    ] 

     ] 

      // Hide the window and remove our reference to it so it will be deleted  [19659000]] 

          windowsForScreens  [[19659073]  ]    19659013]                               [[196590100] to    Screen :    UIScreen )    window    =    windowsForScreens  [  screen ]    otherwise    {   return  } [19659092] window .   IsHidden    =    sanne 
          windowsForScreens  [  screen ]    =    nil 
    } 

      FUNC    application  ]   _    application :    UIA application  
                     [19659000]]]    ->    Bool    {
        
          // Set up the device's  [19659000]]    [19659000] [  ] [ ] 

  ]    [19659000]] [19659000] [19659000] [19659000] [19659000] [19659000]   to :      [19659000]     [19659000] :    UIScreen .   main )] 

          // We need to set up the other screens already linked 
          la    otherScreens    =    UIScreen .   displays .   [filtrere19659010] {   $ 0  ! =    UIScreen .   most important  } 
          otherScreens .    foreach    {   [  screen ) ] 
              setupWindow  (  for :    screen ) 
         ]} 

          // Listen to the display connection alert 
         ] // Set up the new window and attach it to the screen 
          NotificationCenter .   standard 
            . [19659000] AddObserver  (  forName :  :    UIScreen    didConnectNotification  
                          object .    nil  
                          queue .    ] 
         (  Notice )    in 
                            
                              // UIKit is nice enough to give us the screen item 
                              // as represents the recently connected screen 
                              la [19659015] newScreen    =    message .   item    as    UIScreen 
                            
                              even    setupWindow  (  to :    newScreen ) !. 
        } 
        
          // Listen to the screen interruption message. 
          NotificationCenter .   standard .   addObserver  (  forName :    UIScreen .   didDisconnectNotification  
                                                object :    nil  
                                                queue .      [19659000]]    {   [[19659073] Notice )    in 
                                                
                                                  La    NewScreen    =    Notice  .   item    as! [19659000]] [19659000]] [19659000]]   ]  [19659000]] [19659000]] 
     
     
     
     
     
     
       to :    NewScreen  } 
} 

Oh Wow.

Yes, it's very easy.
Create a window, add a root viewer to it, and set the window's screen to the external screen.
When the display is disconnected, just hide and hid the reference of the window so that it can be distributed.

Gracefully Transition

You can mix the user interface mirroring and set up a dedicated external interface by setting up and tearing down the external window in certain phases of interaction with your app.
For example, a social media app could maintain standard mirroring behavior for most of the user interface, but present a full screen mall display when viewing a user's images.
When the external window is not configured, by default iOS will mirror the screen, so tearing down the window will automatically display the mirrored screen again.

Remember that the user could connect or disconnect a screen at any time during the application's execution.
Apple recommends that you listen for these connection / disconnection notifications and gracefully transfer your user interface.
The example they provide is a photo gallery app, where you can select images from a grid to show them in fullscreen.
If the user sees a full-screen image and then plugs on a screen, the app's main screen pops back to the photo pane and selects the selected image while the image appears full screen on the external screen.
The transition makes it clear that the user now controls what appears on the second screen with the device in his hands.
When the screen is disconnected, the app should press the selected image back in full screen on the iOS device screen.

More Considerations

Some warnings about how to add external monitor support to your app:

  • On iPad, only the primary app in multitasking can access external monitors. If your app supports multitasking, make sure you take this and communicate it to the users so that they understand this edge.
  • The external screen does not receive any recording events, so do not enter interactive content on the display.
  • The device does the external display content. This may be a performance here if your app already uses a lot of CPU or GPU power, so be sure to profile your app and optimize as much as possible.
  • If you are using AirPlay mirroring, the device sends compressed video over the network. There will be some flow artifacts on the external display. It may be useful for your app to alert users to this if they expect perfect fidelity (for example, in a pro-content app).
  • External monitor resolutions must be accounted for. Check out the documentation on UIScreen for more information about UIScreenModes.




Source link