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

时间:2010-12-02 11:45:50

标签: iphone cocoa-touch ios4 iphone-sdk-4.0.1

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

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

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

我该怎么办?

请帮助和建议。

感谢。

1 个答案:

答案 0 :(得分:4)

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

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

编辑:如果您确实想要检查设备是否正在充电(而不是100%已满),请在if语句中使用UIDeviceBatteryStateCharging常量。有关详细信息,请参阅the documentation