Yes, there actually is a way.
Somewhere in your app, create an instance of MPVolumeView
. Hold on to in in some instance variable. You don't have to add it as a subview to anything, it simply has to exist.
Then subscribe to the MPVolumeViewWirelessRouteActiveDidChangeNotification
like so:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleWirelessRouteActiveDidChangeNotification:)
name:MPVolumeViewWirelessRouteActiveDidChangeNotification
object:nil];
Add these methods to find out about the state of external displays:
- (BOOL)isAirPlayConnected
{
return _volumeView.isWirelessRouteActive;
}
- (BOOL)isAirPlayMirroringActive
{
if ([self isAirPlayConnected]) {
NSArray *screens = [UIScreen screens];
if ([screens count] > 1) {
return [screens[1] mirroredScreen] == [UIScreen mainScreen];
}
}
return NO;
}
- (BOOL)isAirPlayPlaybackActive
{
return [self isAirPlayConnected] && ![self isAirPlayMirroringActive];
}
- (BOOL)isExternalPlaybackActive
{
if ([self isAirPlayPlaybackActive]) {
return YES;
} else {
NSArray *screens = [UIScreen screens];
if ([screens count] > 1) {
return [screens[1] mirroredScreen] != [UIScreen mainScreen];
}
}
return NO;
}
Additionally you can check for the UIScreenDidConnectNotification
and UIScreenDidDisconnectNotification
notifications. Armed with all of this, you can tell if you are connected to AirPlay, if AirPlay Mirroring is active, if you AirPlay playback (not mirroring) is active or if you are using any external screen without mirroring.