Display one thing on iPad and another on Apple Tv?

依然范特西╮ 提交于 2019-11-28 19:53:50

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!