Verify if iphone was connected to a dock
-
28-04-2021 - |
Question
When I connect the iphone to a dock my app display the message: "Conector dock". I want to detect when the phone is connected with other device and hide the MPVolumeView to avoid those messages.
I am using MPVolumeView as the usual:
MPVolumeView *myVolume = [[MPVolumeView alloc] initWithFrame:CGRectMake(10, 435, 300, 0)];
[myVolume sizeToFit];
[self.view addSubview:myVolume];
[myVolume release];
Could anyone help me?
Solution
I did that by adding the observers that follow:
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
EAAccessoryManager *accessoryMamaner = [EAAccessoryManager sharedAccessoryManager];
[accessoryMamaner registerForLocalNotifications];
[notificationCenter addObserver: self selector: @selector (accessoryDidConnect:) name: EAAccessoryDidConnectNotification object: nil];
[notificationCenter addObserver: self selector: @selector (accessoryDidDisconnect:) name: EAAccessoryDidDisconnectNotification object: nil];
OTHER TIPS
You could monitor the battery state.
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
if ([[UIDevice currentDevice] batteryState] != UIDeviceBatteryStateUnplugged) {
// if you end up in here, then you are connected to some power source
// and you can hide your MPVolumeView
}
More information about the battery state can be found at Apple's UIDevice documentation.
Hope this helps!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow