Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know when iOS device is plugged in?

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...

like image 580
human4 Avatar asked Feb 15 '12 20:02

human4


3 Answers

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.

like image 142
progrmr Avatar answered Oct 27 '22 01:10

progrmr


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];  
like image 45
Sam Avatar answered Oct 26 '22 23:10

Sam


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];
}
like image 25
algal Avatar answered Oct 27 '22 01:10

algal