What I wanted to do is take a log on battery percentage. I know the equivalent in linux is sysctl(hw.acpi.battery.life). However when I went through the man of sysctl in mac os x I did not find the equivalent code to find it. Can some one point out how to do this using objective c. I can call sysctlbyname but that doesn't have this as input in its struct. Kindly help me please
相关问题
- Multiple sockets for clients to connect to
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
Try using IOKit and the IOPowerSources functions. You can use IOPSCopyPowerSourcesInfo() to get a blob, and IOPSCopyPowerSourcesList() to then extract a CFArray out of that, listing the power sources. Finally you can use the IOPSGetPowerSourceDescription() to grab the dictionary.
If you can use the command line, you can use the pmset command
Like so:
Another way to get some more info on battery status is to use NSTask to execute the system_profiler command with SPPowerDataType as the argument. On my Mac, it gives me the following results :
Battery Information:
To retrieve what you specifically, you could either parse the appropriate lines, or generate a plist with this command:
$ system_profiler SPPowerDataType -xml > BatteryInfo.plist
This would make it easier to retrieve the values, based on specific keys.
you may use ioreg on the command line as well. For example:
gives you the current battery status, which may be compared to the max capacity: