Skip to content

Instantly share code, notes, and snippets.

@leminlimez
Created October 7, 2023 16:03
Show Gist options
  • Save leminlimez/ed3e3ee3a287c503c5b834acdc0dfcdc to your computer and use it in GitHub Desktop.
Save leminlimez/ed3e3ee3a287c503c5b834acdc0dfcdc to your computer and use it in GitHub Desktop.
Get iOS Battery Info and Temperature
/*
Note: Requires systemgroup.com.apple.powerlog entitlement in order to work.
*/
// get a dictionary of the battery info
NSDictionary* getBatteryInfo()
{
CFDictionaryRef matching = IOServiceMatching("IOPMPowerSource");
io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault, matching);
CFMutableDictionaryRef prop = NULL;
IORegistryEntryCreateCFProperties(service, &prop, NULL, 0);
NSDictionary* dict = (__bridge_transfer NSDictionary*) prop;
IOObjectRelease(service);
return dict;
}
// getting properties
// many of the properties can be found in this header:
// https://opensource.apple.com/source/xnu/xnu-2422.100.13/iokit/IOKit/pwr_mgt/IOPM.h.auto.html
/*
Below are some examples:
*/
// Battery Temperature
double temp = [batteryInfo[@"Temperature"] doubleValue] / 100.0; // needs to be divided by 100, as the value drops the decimal
// Charger Wattage
int watts = [batteryInfo[@"AdapterDetails"][@"Watts"] longLongValue];
// Charger Current
double current = [batteryInfo[@"AdapterDetails"][@"Current"] doubleValue];
// Device Amperage (off of charger)
double amps = [batteryInfo[@"Amperage"] doubleValue];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment