验证iphone是否已连接到扩展坞

时间:2011-12-12 10:47:08

标签: iphone ios dock mpvolumeview

当我将iphone连接到扩展坞时,我的应用程序会显示消息:“Conector dock”。我想检测手机何时与其他设备连接并隐藏MPVolumeView以避免这些消息。

我正常使用MPVolumeView:

MPVolumeView *myVolume = [[MPVolumeView alloc] initWithFrame:CGRectMake(10, 435, 300, 0)];
[myVolume sizeToFit];
[self.view addSubview:myVolume];
[myVolume release];

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

您可以监控电池状态。

[[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
}

有关电池状态的更多信息,请访问Apple's UIDevice documentation

希望这有帮助!

答案 1 :(得分:0)

我通过添加以下观察者来做到这一点:

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];