Skip to content

Instantly share code, notes, and snippets.

@shepgoba
Last active July 13, 2023 14:06
Show Gist options
  • Save shepgoba/b44007536c184dd445a0596ccfa22ebb to your computer and use it in GitHub Desktop.
Save shepgoba/b44007536c184dd445a0596ccfa22ebb to your computer and use it in GitHub Desktop.
actual battery health algorithm as of ios 13.3
//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);
@RedenticDev
Copy link

returns '0 %' for me, iOS 13.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment