How about this.
UIDeviceBatteryState deviceBatteryState = [UIDevice currentDevice].batteryState;
if (deviceBatteryState == UIDeviceBatteryStateCharging || deviceBatteryState == UIDeviceBatteryStateFull) {
// It is charging
}
From the apple documentation,
UIDeviceBatteryState:
The battery power state of the device.
typedef enum {
UIDeviceBatteryStateUnknown,
UIDeviceBatteryStateUnplugged,
UIDeviceBatteryStateCharging,
UIDeviceBatteryStateFull,
} UIDeviceBatteryState;
UIDeviceBatteryStateUnknown:
The battery state for the device cannot be determined.
UIDeviceBatteryStateUnplugged:
The device is not plugged into power; the battery is losing power.
UIDeviceBatteryStateCharging:
The device is plugged into power and the battery is less than 100% charged.
UIDeviceBatteryStateFull:
The device is plugged into power and the battery is 100% charged.
Update:
Just to answer your question completely. Here is how to detect when a charger is plugged in. There is a property named batteryMonitoringEnabled
.
batteryMonitoringEnabled:
A Boolean value indicating whether battery monitoring is enabled (YES)
or not (NO)
.
Enable battery monitoring if your app needs to be notified of changes to the battery state, or if you want to check the battery charge level.
The default value of this property is NO
, which:
- Disables the posting of battery-related notifications
- Disables the ability to read battery charge level and battery state
This will post the notifications UIDeviceBatteryLevelDidChangeNotification
, and UIDeviceBatteryStateDidChangeNotification
when the battery level changes. You can make use of UIDeviceBatteryStateDidChangeNotification
to detect when the charger is plugged in.