有没有办法知道我的设备(iPhone)何时插入电源,如带有USB端口的电脑或汽车音响系统?我在我的应用程序中使用本地化服务,并且我希望在插入设备时自动更改为kCLLocationAccuracyBestForNavigation
。感谢...
答案 0 :(得分:7)
您可以通过UIDevice class启用电池监控并检查电池状态以查看是否正在充电:
typedef enum {
UIDeviceBatteryStateUnknown,
UIDeviceBatteryStateUnplugged,
UIDeviceBatteryStateCharging,
UIDeviceBatteryStateFull,
} UIDeviceBatteryState;
在启用最佳GPS准确度之前,您需要检查充电或充电。
答案 1 :(得分:3)
您可以注册以在配件连接或断开连接时收到通知。
示例:
[[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(accessoryDidConnect:)
name:EAAccessoryDidConnectNotification
object:nil];
[notificationCenter addObserver:self
selector:@selector(accessoryDidDisconnect:)
name:EAAccessoryDidDisconnectNotification
object:nil];
收到此通知后,您可以使用for循环浏览每个附件,如:
NSArray *accessories = [[EAAccessoryManager sharedAccessoryManager] connectedAccessories];
EAAccessory *accessory = nil;
for (EAAccessory *obj in accessories)
{
// See if you're interested in this particular accessory
}
在某些时候(也许是dealloc),您将要取消注册这些通知。你可以这样做:
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self
name:EAAccessoryDidDisconnectNotification
object:nil];
[notificationCenter removeObserver:self
name:EAAccessoryDidConnectNotification
object:nil];
[[EAAccessoryManager sharedAccessoryManager] unregisterForLocalNotifications];
答案 2 :(得分:3)
检查电池的状态:
UIDeviceBatteryState batteryState = [[UIDevice currentDevice] batteryState];
订阅有关电池状态更改的通知,例如通过调用您自己的操作方法batteryStateChanged
:
- (void) setup {
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
NSNotificationCenter * center= [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(batteryStateChanged)
name:UIDeviceBatteryStateDidChangeNotification
object:nil];
}
请记住在取消分配对象时取消订阅:
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[UIDevice currentDevice] setBatteryMonitoringEnabled:NO];
}