Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if OSX is on battery and not in power

I would like to adjust functionality of my app when in battery mode

How to detect for an application it is in battery mode, etc. when the mode changes?

like image 886
Peter Lapisu Avatar asked Jul 14 '14 09:07

Peter Lapisu


1 Answers

IOPSGetTimeRemainingEstimate function returns amount of remaining power.

Cited from the discussion section:

If attached to an "Unlimited" power source, like AC power or any external source, the return value is kIOPSTimeRemainingUnlimited

If the system is on "Limited" power, like a battery or UPS, but is still calculating the time remaining, which may take several seconds after each system power event (e.g. waking from sleep, or unplugging AC Power), the return value is kIOPSTimeRemainingUnknown

Otherwise, if the system is on "Limited" power and the system has an accurate time remaining estimate, the system returns a CFTimeInterval estimate of the time remaining until the system is out of battery power.

If you require more detailed battery information, use IOPSCopyPowerSourcesInfo> and IOPSGetPowerSourceDescription>.


IOPSNotificationCreateRunLoopSource function can be used to register a callback function on power source related events.

like image 58
eonil Avatar answered Oct 01 '22 07:10

eonil