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

发布于 2025-01-05 11:47:32 字数 142 浏览 0 评论 0原文

有没有办法知道我的设备 (iPhone) 何时插入电源,例如带有 USB 端口的计算机或汽车音响系统?我在应用中使用本地化服务,并且希望在插入设备时自动更改为 kCLLocationAccuracyBestForNavigation。谢谢...

Is there a way to know when my device (iPhone) is plugged in to source power, like a computer or car audio systems with a USB port? I use localization services in my app and I want to change to kCLLocationAccuracyBestForNavigation automatically when the device is plugged. Thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

高冷爸爸 2025-01-12 11:47:32

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

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

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

You can enable battery monitoring thru the UIDevice class and check the battery state to see if it is being charged:

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

You'll want to check for Charging or Full before enabling best GPS accuracy.

谁许谁一生繁华 2025-01-12 11:47:32

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

示例:

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

在某些时候(也许是释放)您将希望取消注册这些通知。你可以这样做:

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self 
                              name:EAAccessoryDidDisconnectNotification 
                            object:nil];
[notificationCenter removeObserver:self 
                              name:EAAccessoryDidConnectNotification 
                            object:nil];
[[EAAccessoryManager sharedAccessoryManager] unregisterForLocalNotifications];  

You can register to be notified when an accessory connects or disconnects.

Example:

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

Once you receive this notification you can use a for loop to go through each accessory like:

NSArray *accessories = [[EAAccessoryManager sharedAccessoryManager] connectedAccessories]; 
EAAccessory *accessory = nil; 

for (EAAccessory *obj in accessories)
{ 
    // See if you're interested in this particular accessory
}

At some point (dealloc perhaps) you will want to unregister for these notifications. You can do this like:

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self 
                              name:EAAccessoryDidDisconnectNotification 
                            object:nil];
[notificationCenter removeObserver:self 
                              name:EAAccessoryDidConnectNotification 
                            object:nil];
[[EAAccessoryManager sharedAccessoryManager] unregisterForLocalNotifications];  
春夜浅 2025-01-12 11:47:32

检查电池状态:

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

To check the state of the battery:

UIDeviceBatteryState batteryState = [[UIDevice currentDevice] batteryState];

To subscribe to notifications about changes in the battery state, for instance by getting a call to your own action method batteryStateChanged:

- (void) setup {
  [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
  NSNotificationCenter * center= [NSNotificationCenter defaultCenter];
  [center addObserver:self
             selector:@selector(batteryStateChanged)
                 name:UIDeviceBatteryStateDidChangeNotification
               object:nil];
}

Remember to unsubscribe when your object is dealloced:

- (void) dealloc
{
   [[NSNotificationCenter defaultCenter] removeObserver:self];
   [[UIDevice currentDevice] setBatteryMonitoringEnabled:NO];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文