iosxcode4.2external-display

ios external display programmatically


i'm trying to support external display on my iOS app without using any .xib files.

right now my code contains this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ 
    if ([[UIScreen screens] count] > 1){
        externalScreen = [[UIScreen screens] objectAtIndex:1];
        UIScreenMode *current = [[[[UIScreen screens]objectAtIndex:1]availableModes]objectAtIndex:0];
        externalScreen.currentMode = current;
        externalWindow = [[UIWindow alloc] initWithFrame:[externalScreen bounds]];
        externalWindow.screen = externalScreen;
        externalWindow.clipsToBounds = YES;

        extController = [[ExternalController alloc] init];
        [externalWindow addSubview:extController.view];
        [externalWindow makeKeyAndVisible];
    }
    self.window = [[UIWindow alloc] initWithFrame:[[[UIScreen screens] objectAtIndex:0] bounds]];

    mainController = [[ViewController alloc] init];
    [self.window addSubview:mainController.view];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
}

when i run this, the TV Out screen will close immediately. If i comment this line:

//self.window = [[UIWindow alloc] initWithFrame:[[[UIScreen screens] objectAtIndex:0] bounds]];

the TV Out screen will work properly, but of course i don't see anything on my simulator screen.

Does anyone have any idea what I should do? Thanks in advance!


Solution

  • I can suggest you try the sample source code TVOut on GITHub - https://github.com/JohnGoodstadt/TVOut.

    It uses similar code to yours but packaged in a class which you can copy over and call from your own code.

    It should solve your display problem. By the way I would not do your code as early as in the 'didFinishLaunchingWithOptions' but later in the first view controller.