我正在尝试在我的应用中启用AirPlay支持.我不是在做视频;我想将外部显示器用作“第二显示器”.
这是我的问题:如果我从AirPlay按钮选择“AppleTV”,我的应用程序不会收到通知.我的应用程序获得通知的唯一一次是当我离开我的应用程序时,转到操作系统级别的AirPlay按钮,在那里选择“AppleTV”并打开镜像.如果我关闭镜像,我的应用程序会被告知外部显示器已经消失.
所以:
>当我从中选择外部显示器时,为什么我的应用程序不会收到通知
在我的应用程序?
>为什么我的应用程序会收到通知
我开镜像时的外部显示……而不是之前?我显然误解了一些东西,但是看起来像镜像应该通知我的应用程序外部显示器已经消失(而不是现在可用,因为操作系统现在应该使用外部显示器进行镜像.)
代码示例如下.在此先感谢您的帮助!
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. // Is there already an external screen? if (UIScreen.screens.count > 1)] { [self prepareExternalScreen:UIScreen.screens.lastObject]; } // Tell us when an external screen is added or removed. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(externalScreenDidConnect:) name:UIScreenDidConnectNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(externalScreenDidDisconnect:) name:UIScreenDidDisconnectNotification object:nil]; self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; self.window.rootViewController = self.viewController; // Add AirPlay control to view controller. MPVolumeView* airplayButtonView = [[MPVolumeView alloc] init]; airplayButtonView.frame = CGRectMake(300,300,50,50); airplayButtonView.backgroundColor = [UIColor blackColor]; airplayButtonView.showsVolumeSlider = NO; airplayButtonView.showsRouteButton = YES; [self.viewController.view addSubview:airplayButtonView]; [self.window makeKeyAndVisible]; return YES; } #pragma mark - External screen handling - (void)externalScreenDidConnect:(NSNotification*)notification { [self prepareExternalScreen:notification.object]; } - (void)externalScreenDidDisconnect:(NSNotification*)notification { // Don't need these anymore. self.externalWindow = nil; } - (void)prepareExternalScreen:(UIScreen*)externalScreen { NSLog(@"PREPPING EXTERNAL SCREEN."); self.viewController.view.backgroundColor = [UIColor blueColor]; CGRect frame = externalScreen.bounds; self.externalWindow = [[UIWindow alloc] initWithFrame:frame]; self.externalWindow.screen = externalScreen; self.externalWindow.hidden = NO; self.externalWindow.backgroundColor = [UIColor redColor]; }