【问题标题】:How to know when iOS device is plugged in?如何知道 iOS 设备何时插入?
【发布时间】:2012-02-15 20:20:34
【问题描述】:

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

【问题讨论】:

    标签: iphone ios localization


    【解决方案1】:

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

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

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

    【讨论】:

    • +1 在UIDevicebatteryState 属性上执行KVO 似乎是实现OP 想要做的最好的方式。
    【解决方案2】:

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

    例子:

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

    【讨论】:

    • @human4 总是乐于提供帮助。如果 UIDevicebatteryState 上的 KVO 有效,那么我会采用这种方式。
    【解决方案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];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      • 2022-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      • 2019-12-10
      相关资源
      最近更新 更多