简体   繁体   English

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

[英]How to detect from an iPhone app whether the battery is in charging or not?

In my iPhone app, there is a requirement to know whether the iPhone is charging or not?在我的 iPhone 应用程序中,需要知道 iPhone 是否正在充电?

If it is charging then I want to display alert for the same.如果它正在充电,那么我想显示相同的警报。

So how can I detect whether the iPhone is charging or not?那么如何检测 iPhone 是否正在充电呢?

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

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

Edit: If you relly want to check if the device is charging (and not 100% full yet), use the UIDeviceBatteryStateCharging constant in the if statement.编辑:如果您真的想检查设备是否正在充电(尚未 100% 充满电),请在 if 语句中使用UIDeviceBatteryStateCharging常量。 See the documentation for more information.有关更多信息,请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM