//通过UIDevice类,可以取得电池剩余量以及充电状态的信息,首先需要设置batteryMonitoringEnabled为YES
UIDevice * device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES;
//0.0-1.0取得失败的适合为-1.0
NSLog(@"%f", device.batteryLevel);
/*
typedef NS_ENUM(NSInteger, UIDeviceBatteryState) {
UIDeviceBatteryStateUnknown,
UIDeviceBatteryStateUnplugged, // on battery, discharging
UIDeviceBatteryStateCharging, // plugged in, less than 100%
UIDeviceBatteryStateFull, // plugged in, at 100%
}; // available in iPhone 3.0
UIDeviceBatteryStateUnknown:无法取得充电状态情况
UIDeviceBatteryStateUnplugged:非充电状态
UIDeviceBatteryStateCharging:充电状态
UIDeviceBatteryStateFull:充满状态(连接充电器充满状态)
*/
NSLog(@"%ld", (long)device.batteryState);
//监视电池剩余电量
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action1:) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
//监视充电状态
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action2:) name:UIDeviceBatteryStateDidChangeNotification object:nil];