iPhone4 how to find out if the power cable is plugged in?

前端 未结 3 1460
眼角桃花
眼角桃花 2021-02-10 14:36

I would like to know if my app is running with an external power cable attached. Is it possible to find out this state at runtime?

An extra question: would this be able

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-10 14:47

    You can detect whether the battery is charging, but that's as close as you can get with existing APIs – there's no way to detect where the power is "coming from", so to speak.

    UIDeviceBatteryState batteryState = [UIDevice currentDevice].batteryState;
    if (batteryState == UIDeviceBatteryStateCharging) {
        // Your code here
    }
    

提交回复
热议问题