如何从iPhone应用程序检测电池是否正在充电?

问题描述 投票:2回答:1

在我的iPhone应用程序中,需要知道iPhone是否正在充电?

如果正在充电,那么我想同样显示警报。

那么如何检测iPhone是否正在充电?

iphone cocoa-touch ios4 iphone-sdk-4.0.1
1个回答
4
投票
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

if ([[UIDevice currentDevice] batteryState] != UIDeviceBatteryStateUnplugged) {
    //Device is connected (charging or fully charged)
}

编辑:如果您只是想检查设备是否正在充电(并且尚未100%充满),请在if语句中使用UIDeviceBatteryStateCharging常量。有关更多信息,请参见the documentation

© www.soinside.com 2019 - 2024. All rights reserved.