问题
I'm currently working on a Backup Application, and I have received a lot of requests for features to be added recently, and the top one of them is adding a checkbox like "Back Up when on Battery Power" like Time Machine has.
So, is there a way I can get the status of the Power Adapter (plugged in and plugged in)? I assume that if one had a Desktop Mac, like iMac, etc, I would probably just get Plugged In all the time. Therefore, I need means of detecting if the computer is a portable or not.
I assume IOKit would be a library to look at, but I simply could not find anything in the docs, that are barely existent anyways on that Framework. Then, since it is an older Carbon Framework, is there a way to register a callback that gets called when that power state changes? That way I can properly implement this checkbox's functionality.
I'd appreciate help in clarifying this subject.
回答1:
IOPowerSources.h has the functions you need. Start with IOPSNotificationCreateRunLoopSource()
to create a run loop source and in your callback interrogate the power source info starting with IOPSCopyPowerSourcesInfo()
and working your way down. There may be better examples, but this file appears to be a reasonable demonstration of how it's done; enough to get you started.
来源:https://stackoverflow.com/questions/4552286/cocoa-get-power-adapter-status