UIScreen Notifications for connecting external display in iOS

UIScreen class has three main notifications which gets triggered when a new screen (external display) is attached to the device.

UIScreenDidConnectNotification

This notification is posted when a new screen is connected to the device. The object of the notification is the UIScreen object representing the new screen. There is no userInfo dictionary. Connection notifications are not sent for screens that are already present when the application is launched. The application can instead use the Simple code to listen to the notification

- (void)awakeFromNib{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(screenDidConnect:) name:UIScreenDidConnectNotification object:nil]; 
    [center addObserver:self selector:@selector(screenDidDisconnect:) name:UIScreenDidDisconnectNotification object:nil]; 
    [center addObserver:self selector:@selector(screenModeDidChange:) name:UIScreenModeDidChangeNotification object:nil]; 
}

- (void) screenDidConnect:(NSNotification *)aNotification{
    NSLog(@"A new screen got connected: %@", [aNotification object]);
}

- (void) screenDidDisconnect:(NSNotification *)aNotification{
    NSLog(@"A screen got disconnected: %@", [aNotification object]);
}

- (void) screenModeDidChange:(NSNotification *)aNotification{
    UIScreen *someScreen = [aNotification object];
    NSLog(@"The screen mode for a screen did change: %@", [someScreen currentMode]);

}

- (void)dealloc{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center removeObserver:self name:UIScreenDidConnectNotification object:nil];
    [center removeObserver:self name:UIScreenDidDisconnectNotification object:nil];
    [center removeObserver:self name:UIScreenModeDidChangeNotification object:nil];
	
	[super dealloc];
}

If anyone is interested in screen mirroring in iPhone / iPad check this UIScreen Additions.

By Imthiaz

Programmer, SAAS, CMS & CRM framework designer, Love Linux & Apple products, Currently addicted to mobile development & working @bluebeetle