issue with airplay - external screen overrides main screen
===

I am trying to create a very simple application to test airplay mirroring and split screen. My main view controller has a switch that suppose to switch between mirroring mode and split screen mode, all this after enabling mirroring on the system dock (so first - enable mirroring on the main dock, then use the switch to go from mirror mode to split screen mode from the app). The first switch works ok - I see the new widow on the Apple TV, and the device screen remains the same. Switching again, cause the  external view to appear on the device screen. At this point, there is nothing I can do since the switch is no longer there...(the external screen is just a blank screen for the sake of the exercise).

Can anyone please help me understand what I am doing wrong? I am pretty sure it's something small, but i would not be surprised if I am totally off...:/

Some of my code:

AppDelegate:


    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
    }


viewcontroller.m:


screenDidChange is called from viewDidLoad. switchAction is called when tapping on the switch.

     - (void)screenDidChange:(NSNotification *)notification
     {
    
    NSArray *screens;
    screens = [UIScreen screens];
    NSUInteger screenCount = [screens count];
    
    if (screenCount > 1) 
    {
        self.extScreen = [screens objectAtIndex:1];    
    }
    else 
    {
        // Release external screen and window
        self.extScreen = nil;
        self.mainWindow = nil;
        [self externalWindow:self.mainWindow];
    }
    }


    - (IBAction)switchAction:(id)sender {
        
    NSArray *screens;
    screens = [UIScreen screens];
    NSUInteger screenCount = [screens count];

    
    
    if ([sender isOn] == YES)
    {
        //Mirror mode
        self.extScreen = [screens objectAtIndex:1]; 
        [mainWindow makeKeyAndVisible];
    }
    else {
        //Split Screen mode
        self.extScreen = [screens objectAtIndex:0];
        [mainWindow makeKeyAndVisible];
    }
    
    self.extWindow2.hidden = NO;
    
    
    if (extWindow2 == nil || !CGRectEqualToRect(extWindow2.bounds, [extScreen bounds])) 
    {
        extWindow2 = [[UIWindow alloc] initWithFrame:[extScreen bounds]];
        extWindow2.screen = extScreen;
        UIView *view = [[UIView alloc] initWithFrame:[extWindow2 frame]];
        view.backgroundColor = [UIColor blueColor];
        [extWindow2 addSubview:view];
        [extWindow2 makeKeyAndVisible];
        
    }
    }



