Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何知道iOS设备何时插入?_Iphone_Ios_Localization - Fatal编程技术网

Iphone 如何知道iOS设备何时插入?

Iphone 如何知道iOS设备何时插入?,iphone,ios,localization,Iphone,Ios,Localization,有没有办法知道我的设备(iPhone)何时插入电源,比如电脑或带有USB端口的汽车音响系统?我在我的应用程序中使用本地化服务,我想在设备插入时自动切换到kCLLocationAccuracyBestForNavigation。谢谢…您可以注册,以便在附件连接或断开时收到通知 例如: [[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications]; NSNotificationCenter *notificat

有没有办法知道我的设备(iPhone)何时插入电源,比如电脑或带有USB端口的汽车音响系统?我在我的应用程序中使用本地化服务,我想在设备插入时自动切换到
kCLLocationAccuracyBestForNavigation
。谢谢…

您可以注册,以便在附件连接或断开时收到通知

例如:

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

您可以通过启用电池监控,并检查电池状态以查看是否正在充电:

typedef enum {
    UIDeviceBatteryStateUnknown,
    UIDeviceBatteryStateUnplugged,
    UIDeviceBatteryStateCharging,
    UIDeviceBatteryStateFull,
} UIDeviceBatteryState;

在启用最佳GPS精度之前,您需要检查电池是否充满电。

要检查电池状态:

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

+1在
UIDevice
batteryState
属性上执行KVO似乎是实现OP想要做的事情的最佳方式。@human4总是乐于提供帮助。如果
UIDevice
batteryState
上的KVO工作正常,那么我会这么做。