title: Re-enable mirroring on iOS
body: <p>In my iOS app I need to display custom content on external display (using AirPlay) as well as mirroring some screens on TV.
  For presenting custom content I use code from <a href="https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/WindowAndScreenGuide/UsingExternalDisplay/UsingExternalDisplay.html#//apple_ref/doc/uid/TP40012555-CH3-SW2" rel="nofollow noreferrer">Multiple Display Programming Guide for iOS</a> and it works well: while my iPad is in 'mirror' AirPlay mode I'm able to show some stuff on the TV. However, documentation says6</p>
  <blockquote>
  <p>To re-enable mirroring after displaying unique content, simply remove the window you created from the appropriate screen object.</p>
  </blockquote>
  <p>And this part isn't working at all. I just cannot destroy my window that I use to display content on external screen. Here's the code:</p>
  <pre><code>- (void) destroySecondWindow{
      if (secondWindow){
          for( UIView* view in secondWindow.subviews ){
              [view removeFromSuperview];
          }        
      
          secondWindow.backgroundColor = [UIColor clearColor];
          secondWindow.hidden = YES;
      
          // Hide and then delete the window.
          [secondWindow removeFromSuperview];
          secondWindow = nil;
      
      }
  }
  </code></pre>
  <p>As far as unique content should be displayed only when one particular view controller is visible, I'm trying to destroy external window like this:</p>
  <pre><code> - (void) viewWillDisappear:(BOOL)animated{
       [self destroySecondWindow];
   }
  </code></pre>
  <p>Here's how I create second window:</p>
  <pre><code>- (void) createSecondWindowForScreen:(UIScreen*)screen{
      if( screen == nil || secondWindow != nil ){
          return;
      }
      
      CGRect        screenBounds = screen.bounds;
      
      secondWindow = [[UIWindow alloc] initWithFrame:screenBounds];
      secondWindow.backgroundColor = [UIColor blueColor];
      secondWindow.screen = screen;
  
      [secondWindow setHidden:NO];
  }
  </code></pre>
  <p>So the question is: does anybody know how to re-enable screen mirroring after displaying unique content on TV?</p>
  <p>Thanks in advance!</p>