Get Battery Charge Information Programmatically

ぐ巨炮叔叔 提交于 2019-12-11 15:06:36

问题


(Xcode 6, OSX 10.10.3) How do I get the battery charge information of a Mac (Charge Status, Battery level, Cycle count, Maximum charge in mAH) programmatically?


回答1:


Hope this'll help you:

  1. It uses non-approved calls, so don’t post your application with this code or you may be rejected!

    http://blog.coriolis.ch/2009/02/14/reading-the-battery-level-programmatically/

  2. From Apple; so don't worry about this code: https://developer.apple.com/library/ios/samplecode/BatteryStatus/Introduction/Intro.html (With complete sample code)




回答2:


Use PFSystemKit. It is a framework that gives you about anything (besides sensors/smc info), including battery life, cycle, temperature and other stuff, just import the framework. Then add #import <PFSystemKit/PFSystemKit.h>. Then just add this code for battery info, make sure to change "cycleCount" to something else in a different case

 PFSystemKit *systemKit = [PFSystemKit investigate];
 NSString *batteryCycle = [systemKit.batteryReport cycleCount];
 [label setStringValue:batteryCycle];

https://github.com/perfaram/PFSystemKit

PS, sorry for a late response :P, and just a heads up I have a macmini so my output is NULL!



来源:https://stackoverflow.com/questions/30179473/get-battery-charge-information-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!