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 screens method to get the current set of screens at launch time.

UIScreenDidDisconnectNotification

This notification is posted when a screen is disconnected from the device. The object of the notification is the UIScreen object that represented the now disconnected screen. There is no userInfo dictionary.

UIScreenModeDidChangeNotification

This notification is posted when the current mode of a screen changes. The object of the notification is the UIScreen object whose currentMode property changed. There is no userInfo dictionary. Clients can use this notification to detect changes in the screen resolution.

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