Skip to content

Instantly share code, notes, and snippets.

@martinpilch
Last active October 12, 2015 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinpilch/4011154 to your computer and use it in GitHub Desktop.
Save martinpilch/4011154 to your computer and use it in GitHub Desktop.
AirPlay video mirroring
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//... Initialization of application
//Screen notification handlers
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(handleScreenDidConnectNotification:)
name:UIScreenDidConnectNotification object:nil];
[center addObserver:self selector:@selector(handleScreenDidDisconnectNotification:)
name:UIScreenDidDisconnectNotification object:nil];
//Init second screen if presented
[self initSecondScreen];
return YES;
}
#pragma mark -
#pragma mark Display handling
- (void)handleScreenDidConnectNotification:(NSNotification*)aNotification {
//Handle AirPlay device connection
[self initSecondScreen];
}
- (void)handleScreenDidDisconnectNotification:(NSNotification*)aNotification {
//Handle AirPlay device disconnection
if (self.secondaryWindow)
{
// Hide and then delete the window.
self.secondaryWindow.hidden = YES;
self.secondaryWindow = nil;
}
}
- (void)initSecondScreen
{
if ([[UIScreen screens] count] > 1)
{
UIScreen *screen = [[UIScreen screens] lastObject];
//Init second window do display content
if (!self.secondaryWindow)
{
self.secondaryWindow = [[UIWindow alloc] initWithFrame:screen.bounds];
self.secondaryWindow.screen = screen;
self.secondaryWindow.hidden = NO;
}
//Controller to display content on AirPlay device
self.secondaryWindow.rootViewController = [[AirPlayerViewController alloc] init];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment