Last active
July 13, 2023 14:06
-
-
Save shepgoba/b44007536c184dd445a0596ccfa22ebb to your computer and use it in GitHub Desktop.
actual battery health algorithm as of ios 13.3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//requires: com.apple.private.iokit.batterydata entitlement | |
extern "C" | |
CFArrayRef IOPSCopyPowerSourcesByType(int type); | |
int healthPercent; | |
NSArray *sources = (__bridge NSArray *)IOPSCopyPowerSourcesByType(1); | |
NSDictionary *batteryDict = sources[0]; | |
if (sources && sources.count && batteryDict[@"Maximum Capacity Percent"]) { | |
healthPercent = [batteryDict[@"Maximum Capacity Percent"] intValue]; | |
} else { | |
healthPercent = -1; | |
} | |
double constraintedHealthPercent = fmax(fmin(healthPercent / 100.0, 1.0), 0.0) * 100; | |
int finalPercent = (int)constraintedHealthPercent; | |
NSLog(@"correctedHealthPercent: %i", finalPercent); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
returns '0 %' for me, iOS 13.7