Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 to differentiate between true USB power and those external "battery packs"?

Thank you!

like image 765
Alex Stone Avatar asked Dec 16 '22 08:12

Alex Stone


1 Answers

Use UIDevice property batteryState:

[[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateCharging

From UIDevice Docs:

typedef enum {
    UIDeviceBatteryStateUnknown,
    UIDeviceBatteryStateUnplugged,
    UIDeviceBatteryStateCharging,
    UIDeviceBatteryStateFull,
} UIDeviceBatteryState;

As for your 2nd question. I don't believe you can determine any difference between a battery pack and a wall charger since the above UIDeviceBatteryState flags are the only "states" a device battery can report. So both a battery pack and wall charger would appear as either UIDeviceBatteryStateCharging or UIDeviceBatteryStateFull (or UIDeviceBatteryStateUnplugged if the battery pack is plugged in but out of juice).

like image 156
chown Avatar answered Jan 18 '23 14:01

chown