Developer
Developer

Reputation: 4321

iPad simulator and external screen

i have an application that i wish to show on external screen.

The problem is that when i go Hardware -> External displays and select one of them - the events aren't triggered. Why?

This also doesn't get entered:

if ([[UIScreen screens] count] > 1)

So i have added next code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
     //SOME CODE ...
     [self checkForExistingScreenAndInitializeIfPresent];
     [self setUpScreenConnectionNotificationHandlers];
     return YES:
}

- (void)checkForExistingScreenAndInitializeIfPresent
{
if ([[UIScreen screens] count] > 1)
{
    // Get the screen object that represents the external display.
    UIScreen *secondScreen = [[UIScreen screens] objectAtIndex:1];
    // Get the screen's bounds so that you can create a window of the correct size.
    CGRect screenBounds = secondScreen.bounds;

    self.secondWindow = [[UIWindow alloc] initWithFrame:screenBounds];
    self.secondWindow.screen = secondScreen;

    self.externalWindow=[[ExternalDisplayViewController alloc]initWithNibName:@"ExternalDisplayViewController" bundle:nil];
    self.externalWindow.view.frame=screenBounds;

    self.secondWindow.rootViewController=self.externalWindow;
    // Set up initial content to display...
    // Show the window.
    self.secondWindow.hidden = NO;
    }
}

- (void)setUpScreenConnectionNotificationHandlers
{
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

[center addObserver:self selector:@selector(handleScreenDidConnectNotification:)
               name:UIScreenDidConnectNotification object:nil];
[center addObserver:self selector:@selector(handleScreenDidDisconnectNotification:)
               name:UIScreenDidDisconnectNotification object:nil];
}

ADDITION:

Just tried to add code in ViewDidLoad

Added this:

// Check for external screen.
if ([[UIScreen screens] count] > 1)
{

}
else {
}

Have the opened external display and simulator - does't enter the IF block

Upvotes: 4

Views: 2967

Answers (4)

afinlayson
afinlayson

Reputation: 241

This was driving me nuts. I'm on Version 10.0 (10A255) and it wasn't working. The reason why is I was looking at application:didFinishLaunchingWithOptions: for a UIScreen.screens.count > 1

This will always be 1

Instead try this

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
   let center = NotificationCenter.default
   center.addObserver(self, selector: #selector(didConnect(notification:)), name: UIScreen.didConnectNotification, object: nil)
   return true
}

@objc func didConnect(notification: Notification) {
    if UIScreen.screens.count > 1 {
        if let screen = UIScreen.screens.last {
            let window = UIWindow(frame: screen.bounds)
            window.screen = screen

            let vc = UIViewController(nibName: nil, bundle: nil)
            vc.view.backgroundColor = .red

            window.isHidden = false
            window.rootViewController = vc
            self.secondWindow = window // Will not show unless window variable is retained.
        }
    }
}

Upvotes: 0

Developer
Developer

Reputation: 4321

The problem was in beta versions of OS and Xcode. Downgraded back to Maverics - and all works like a charm

Upvotes: 0

nikolovski
nikolovski

Reputation: 4049

Here's my complete code in my UIViewController subclass. I have checked it with the 7.1 simulator and it works for me (I start the app, and then initialize the external display once it's already running):

- (void)viewDidLoad {
    // Other viewDidLoad code…
    // Check and initialize big screen
    [self checkForExistingScreenAndInitializeIfPresent]; 
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // Register for second screen notifications
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(handleScreenDidConnectNotification:)
               name:UIScreenDidConnectNotification object:nil];
    [center addObserver:self selector:@selector(handleScreenDidDisconnectNotification:)
               name:UIScreenDidDisconnectNotification object:nil];
}

- (void)handleScreenDidConnectNotification:(NSNotification *)notification {
    [self checkForExistingScreenAndInitializeIfPresent];
}

- (void)handleScreenDidDisconnectNotification:(NSNotification *)notification {
    [self checkForExistingScreenAndInitializeIfPresent];
}

- (void)checkForExistingScreenAndInitializeIfPresent {
    if ([[UIScreen screens] count] > 1) {
        // Get the screen object that represents the external display.
        UIScreen *secondScreen = [[UIScreen screens] objectAtIndex:1];
        secondScreen.currentMode = secondScreen.preferredMode;
        secondScreen.overscanCompensation = 3;
        // Get the screen's bounds so that you can create a window of the correct size.
        CGRect screenBounds = CGRectMake(secondScreen.bounds.origin.x,
                                         secondScreen.bounds.origin.y,
                                         secondScreen.currentMode.size.width,
                                         secondScreen.currentMode.size.height);

        UIWindow *secondWindow = [[UIWindow alloc] initWithFrame:screenBounds];
        secondWindow.screen = secondScreen;
        // Setup external VC
        [ExternalScreenViewController sharedExternalScreen].window = secondWindow;
        // Set VC for second window
        secondWindow.rootViewController = [ExternalScreenViewController sharedExternalScreen];
        // Show the window.
        secondWindow.hidden = NO;
    } else {
        // What to do if disconnected
    }
} 

It should work with minor changes.

Upvotes: 0

Kumar KL
Kumar KL

Reputation: 15335

The problem might with the initialisation of the ExternalDisplayViewController :

self.externalWindow=[[ExternalDisplayViewController alloc]initWithNibName:@"ExternalDisplayViewController" bundle:nil];

Try this :

[[ExternalDisplayViewController alloc]initWithNibName:@"ExternalDisplayViewController" bundle:nil];
 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    self.externalWindow= = [storyboard instantiateViewControllerWithIdentifier:@"ExternalDisplayView"];

and also, you need to override this handleScreenDidConnectNotification

-(void)handleScreenDidConnectNotification : (NSNotification *)aNotification{
    UIScreen *newScreen = [aNotification object];
    CGRect screenBounds = newScreen.bounds;
    self.alertForNotifyDisplay =  [[UIAlertView alloc] initWithTitle:@"External Display Connected." message:Nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [self.alertForNotifyDisplay show];

    if (!self.extWindow) {
        self.extWindow  = [[UIWindow alloc] initWithFrame:screenBounds];
        self.extWindow.screen = newScreen;

        [self checkForExistingScreenAndInitializeIfPresent];
    }

}

Upvotes: 0

Related Questions