Display one thing on iPad and another on Apple Tv?

后端 未结 1 486
轮回少年
轮回少年 2020-12-14 05:02

I have an app idea, but I\'m not sure if it\'s possible.

I was wondering if I\'m able to display one thing on the iPad ( or iPhone ) screen, and something totally di

1条回答
  •  醉梦人生
    2020-12-14 05:39

    Turns out that is is pretty simple to support two screens: the primary screen of the iOS device and a secondary screen (either an external display or mirroring on an Apple TV).

    Based on information from the blog post Creating a Dual-Screen AirPlay Experience for iOS and Apple TV, you don't need to do much.

    Basically you need to check the screens property from UIScreen. There are also notifications you should listen for (UIScreenDidConnectNotification and UIScreenDidDisconnectNotification) so you know if the number of screens changes while your app is running.

    Once you have a second screen, you need to create a new window for it. Code like the following can be used:

    if ([UIScreen screens].count > 1) {
        if (!_secondWin) {
            UIScreen *screen = [UIScreen screens][1];
            _secondWin = [[UIWindow alloc] initWithFrame:screen.bounds];
            _secondWin.screen = screen;
        }
    }
    

    where _secondWin is a UIWindow ivar.

    Once the window is setup, create a view controller, make it the window's root view controller, and show the window:

    SomeViewController *vc = [[SomeViewController alloc] init...];
    _secondWin.rootViewController = vc;
    _secondWin.hidden = NO;
    

    This is pretty much it other than proper handling of the notifications. Keep in mind that you can't get any touch events on the 2nd display so make sure whatever you show is basically display-only.

    Depending on your app, you might have the 2nd screen/window being used throughout the lifetime of the app (as long as the 2nd screen is available any way). Or you might only create and use the 2nd window/screen under certain circumstances. When you don't setup the 2nd window/screen, your app will simply be mirrored to the 2nd display or Apple TV.

    The last piece is to turn on mirroring to the Apple TV. This is done on the iOS device, not in the app.

    The blog post I linked has a few more details worth reviewing.

    0 讨论(0)
提交回复
热议问题