iPad simulator and external screen












3














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










share|improve this question




















  • 1




    I think you should have this code in the view controller, not in the app delegate. I have similar code to yours and it works perfectly.
    – Marko Nikolovski
    Jun 27 '14 at 8:57










  • Downloaded source code from here: mattgemmell.com/ipad-vga-output also doesn't work. Maybe i have to enable something IN the simulator?
    – Cheese
    Jun 27 '14 at 9:02










  • I'm almost sure you have to have this code in a UIViewController subclass. The view controller "owns" all of the views, internal & external.
    – Marko Nikolovski
    Jun 27 '14 at 9:13










  • I just checked my old code, which is very similar to what you have (just contained in a view controller), and it works as expected.
    – Marko Nikolovski
    Jun 27 '14 at 9:13










  • This line of code [self checkForExistingScreenAndInitializeIfPresent]; should be present in the viewDidLoad: and notification handlers.
    – Marko Nikolovski
    Jun 27 '14 at 9:14
















3














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










share|improve this question




















  • 1




    I think you should have this code in the view controller, not in the app delegate. I have similar code to yours and it works perfectly.
    – Marko Nikolovski
    Jun 27 '14 at 8:57










  • Downloaded source code from here: mattgemmell.com/ipad-vga-output also doesn't work. Maybe i have to enable something IN the simulator?
    – Cheese
    Jun 27 '14 at 9:02










  • I'm almost sure you have to have this code in a UIViewController subclass. The view controller "owns" all of the views, internal & external.
    – Marko Nikolovski
    Jun 27 '14 at 9:13










  • I just checked my old code, which is very similar to what you have (just contained in a view controller), and it works as expected.
    – Marko Nikolovski
    Jun 27 '14 at 9:13










  • This line of code [self checkForExistingScreenAndInitializeIfPresent]; should be present in the viewDidLoad: and notification handlers.
    – Marko Nikolovski
    Jun 27 '14 at 9:14














3












3








3







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










share|improve this question















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







ios ipad ios-simulator external-display






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 27 '14 at 9:23







Cheese

















asked Jun 27 '14 at 8:36









CheeseCheese

1,96852355




1,96852355








  • 1




    I think you should have this code in the view controller, not in the app delegate. I have similar code to yours and it works perfectly.
    – Marko Nikolovski
    Jun 27 '14 at 8:57










  • Downloaded source code from here: mattgemmell.com/ipad-vga-output also doesn't work. Maybe i have to enable something IN the simulator?
    – Cheese
    Jun 27 '14 at 9:02










  • I'm almost sure you have to have this code in a UIViewController subclass. The view controller "owns" all of the views, internal & external.
    – Marko Nikolovski
    Jun 27 '14 at 9:13










  • I just checked my old code, which is very similar to what you have (just contained in a view controller), and it works as expected.
    – Marko Nikolovski
    Jun 27 '14 at 9:13










  • This line of code [self checkForExistingScreenAndInitializeIfPresent]; should be present in the viewDidLoad: and notification handlers.
    – Marko Nikolovski
    Jun 27 '14 at 9:14














  • 1




    I think you should have this code in the view controller, not in the app delegate. I have similar code to yours and it works perfectly.
    – Marko Nikolovski
    Jun 27 '14 at 8:57










  • Downloaded source code from here: mattgemmell.com/ipad-vga-output also doesn't work. Maybe i have to enable something IN the simulator?
    – Cheese
    Jun 27 '14 at 9:02










  • I'm almost sure you have to have this code in a UIViewController subclass. The view controller "owns" all of the views, internal & external.
    – Marko Nikolovski
    Jun 27 '14 at 9:13










  • I just checked my old code, which is very similar to what you have (just contained in a view controller), and it works as expected.
    – Marko Nikolovski
    Jun 27 '14 at 9:13










  • This line of code [self checkForExistingScreenAndInitializeIfPresent]; should be present in the viewDidLoad: and notification handlers.
    – Marko Nikolovski
    Jun 27 '14 at 9:14








1




1




I think you should have this code in the view controller, not in the app delegate. I have similar code to yours and it works perfectly.
– Marko Nikolovski
Jun 27 '14 at 8:57




I think you should have this code in the view controller, not in the app delegate. I have similar code to yours and it works perfectly.
– Marko Nikolovski
Jun 27 '14 at 8:57












Downloaded source code from here: mattgemmell.com/ipad-vga-output also doesn't work. Maybe i have to enable something IN the simulator?
– Cheese
Jun 27 '14 at 9:02




Downloaded source code from here: mattgemmell.com/ipad-vga-output also doesn't work. Maybe i have to enable something IN the simulator?
– Cheese
Jun 27 '14 at 9:02












I'm almost sure you have to have this code in a UIViewController subclass. The view controller "owns" all of the views, internal & external.
– Marko Nikolovski
Jun 27 '14 at 9:13




I'm almost sure you have to have this code in a UIViewController subclass. The view controller "owns" all of the views, internal & external.
– Marko Nikolovski
Jun 27 '14 at 9:13












I just checked my old code, which is very similar to what you have (just contained in a view controller), and it works as expected.
– Marko Nikolovski
Jun 27 '14 at 9:13




I just checked my old code, which is very similar to what you have (just contained in a view controller), and it works as expected.
– Marko Nikolovski
Jun 27 '14 at 9:13












This line of code [self checkForExistingScreenAndInitializeIfPresent]; should be present in the viewDidLoad: and notification handlers.
– Marko Nikolovski
Jun 27 '14 at 9:14




This line of code [self checkForExistingScreenAndInitializeIfPresent]; should be present in the viewDidLoad: and notification handlers.
– Marko Nikolovski
Jun 27 '14 at 9:14












4 Answers
4






active

oldest

votes


















0














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];
}

}





share|improve this answer





















  • You didn't get it - the problem is that it does't enter IF block - doesn't detect simulated external display
    – Cheese
    Jun 27 '14 at 8:49










  • Check the edited one. You need to handle the connection and need to call checkForExistingScreenAndInitializeIfPresent
    – Kumar KL
    Jun 27 '14 at 8:50










  • I have the written handlers, just didn't paste them here. The problem is that, that code doesn't enter them if i disable or enable external display
    – Cheese
    Jun 27 '14 at 8:51












  • Downloaded source code from here: mattgemmell.com/ipad-vga-output also doesn't work. Maybe i have to enable something IN the simulator?
    – Cheese
    Jun 27 '14 at 9:03










  • There is nothing to do with the simulator, except enabling . Further all you need to do with the code .
    – Kumar KL
    Jun 28 '14 at 9:57



















0














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.






share|improve this answer





























    0














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






    share|improve this answer





























      0














      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.
      }
      }
      }





      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24447427%2fipad-simulator-and-external-screen%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        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];
        }

        }





        share|improve this answer





















        • You didn't get it - the problem is that it does't enter IF block - doesn't detect simulated external display
          – Cheese
          Jun 27 '14 at 8:49










        • Check the edited one. You need to handle the connection and need to call checkForExistingScreenAndInitializeIfPresent
          – Kumar KL
          Jun 27 '14 at 8:50










        • I have the written handlers, just didn't paste them here. The problem is that, that code doesn't enter them if i disable or enable external display
          – Cheese
          Jun 27 '14 at 8:51












        • Downloaded source code from here: mattgemmell.com/ipad-vga-output also doesn't work. Maybe i have to enable something IN the simulator?
          – Cheese
          Jun 27 '14 at 9:03










        • There is nothing to do with the simulator, except enabling . Further all you need to do with the code .
          – Kumar KL
          Jun 28 '14 at 9:57
















        0














        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];
        }

        }





        share|improve this answer





















        • You didn't get it - the problem is that it does't enter IF block - doesn't detect simulated external display
          – Cheese
          Jun 27 '14 at 8:49










        • Check the edited one. You need to handle the connection and need to call checkForExistingScreenAndInitializeIfPresent
          – Kumar KL
          Jun 27 '14 at 8:50










        • I have the written handlers, just didn't paste them here. The problem is that, that code doesn't enter them if i disable or enable external display
          – Cheese
          Jun 27 '14 at 8:51












        • Downloaded source code from here: mattgemmell.com/ipad-vga-output also doesn't work. Maybe i have to enable something IN the simulator?
          – Cheese
          Jun 27 '14 at 9:03










        • There is nothing to do with the simulator, except enabling . Further all you need to do with the code .
          – Kumar KL
          Jun 28 '14 at 9:57














        0












        0








        0






        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];
        }

        }





        share|improve this answer












        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];
        }

        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jun 27 '14 at 8:46









        Kumar KLKumar KL

        11.3k93354




        11.3k93354












        • You didn't get it - the problem is that it does't enter IF block - doesn't detect simulated external display
          – Cheese
          Jun 27 '14 at 8:49










        • Check the edited one. You need to handle the connection and need to call checkForExistingScreenAndInitializeIfPresent
          – Kumar KL
          Jun 27 '14 at 8:50










        • I have the written handlers, just didn't paste them here. The problem is that, that code doesn't enter them if i disable or enable external display
          – Cheese
          Jun 27 '14 at 8:51












        • Downloaded source code from here: mattgemmell.com/ipad-vga-output also doesn't work. Maybe i have to enable something IN the simulator?
          – Cheese
          Jun 27 '14 at 9:03










        • There is nothing to do with the simulator, except enabling . Further all you need to do with the code .
          – Kumar KL
          Jun 28 '14 at 9:57


















        • You didn't get it - the problem is that it does't enter IF block - doesn't detect simulated external display
          – Cheese
          Jun 27 '14 at 8:49










        • Check the edited one. You need to handle the connection and need to call checkForExistingScreenAndInitializeIfPresent
          – Kumar KL
          Jun 27 '14 at 8:50










        • I have the written handlers, just didn't paste them here. The problem is that, that code doesn't enter them if i disable or enable external display
          – Cheese
          Jun 27 '14 at 8:51












        • Downloaded source code from here: mattgemmell.com/ipad-vga-output also doesn't work. Maybe i have to enable something IN the simulator?
          – Cheese
          Jun 27 '14 at 9:03










        • There is nothing to do with the simulator, except enabling . Further all you need to do with the code .
          – Kumar KL
          Jun 28 '14 at 9:57
















        You didn't get it - the problem is that it does't enter IF block - doesn't detect simulated external display
        – Cheese
        Jun 27 '14 at 8:49




        You didn't get it - the problem is that it does't enter IF block - doesn't detect simulated external display
        – Cheese
        Jun 27 '14 at 8:49












        Check the edited one. You need to handle the connection and need to call checkForExistingScreenAndInitializeIfPresent
        – Kumar KL
        Jun 27 '14 at 8:50




        Check the edited one. You need to handle the connection and need to call checkForExistingScreenAndInitializeIfPresent
        – Kumar KL
        Jun 27 '14 at 8:50












        I have the written handlers, just didn't paste them here. The problem is that, that code doesn't enter them if i disable or enable external display
        – Cheese
        Jun 27 '14 at 8:51






        I have the written handlers, just didn't paste them here. The problem is that, that code doesn't enter them if i disable or enable external display
        – Cheese
        Jun 27 '14 at 8:51














        Downloaded source code from here: mattgemmell.com/ipad-vga-output also doesn't work. Maybe i have to enable something IN the simulator?
        – Cheese
        Jun 27 '14 at 9:03




        Downloaded source code from here: mattgemmell.com/ipad-vga-output also doesn't work. Maybe i have to enable something IN the simulator?
        – Cheese
        Jun 27 '14 at 9:03












        There is nothing to do with the simulator, except enabling . Further all you need to do with the code .
        – Kumar KL
        Jun 28 '14 at 9:57




        There is nothing to do with the simulator, except enabling . Further all you need to do with the code .
        – Kumar KL
        Jun 28 '14 at 9:57













        0














        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.






        share|improve this answer


























          0














          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.






          share|improve this answer
























            0












            0








            0






            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.






            share|improve this answer












            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jun 27 '14 at 10:29









            Marko NikolovskiMarko Nikolovski

            3,3012535




            3,3012535























                0














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






                share|improve this answer


























                  0














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






                  share|improve this answer
























                    0












                    0








                    0






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






                    share|improve this answer












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







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jul 15 '14 at 5:46









                    CheeseCheese

                    1,96852355




                    1,96852355























                        0














                        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.
                        }
                        }
                        }





                        share|improve this answer


























                          0














                          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.
                          }
                          }
                          }





                          share|improve this answer
























                            0












                            0








                            0






                            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.
                            }
                            }
                            }





                            share|improve this answer












                            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.
                            }
                            }
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 23 '18 at 18:44









                            afinlaysonafinlayson

                            8112




                            8112






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24447427%2fipad-simulator-and-external-screen%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                                Calculate evaluation metrics using cross_val_predict sklearn

                                Insert data from modal to MySQL (multiple modal on website)