在我的iPhone应用程序中,需要知道iPhone是否正在充电?
如果正在充电,那么我想同样显示警报。
那么如何检测iPhone是否正在充电?
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
if ([[UIDevice currentDevice] batteryState] != UIDeviceBatteryStateUnplugged) {
//Device is connected (charging or fully charged)
}
编辑:如果您只是想检查设备是否正在充电(并且尚未100%充满),请在if语句中使用UIDeviceBatteryStateCharging
常量。有关更多信息,请参见the documentation。