Skip to content

Instantly share code, notes, and snippets.

@JALsnipe
Last active November 5, 2021 13:47
Show Gist options
  • Save JALsnipe/baeb56c704c920272621fdf8ae8e805f to your computer and use it in GitHub Desktop.
Save JALsnipe/baeb56c704c920272621fdf8ae8e805f to your computer and use it in GitHub Desktop.
Script to get iOS device battery info from the BatteryCenter private framework
import Foundation
guard case let batteryCenterHandle = dlopen("/System/Library/PrivateFrameworks/BatteryCenter.framework/BatteryCenter", RTLD_LAZY) where batteryCenterHandle != nil else {
fatalError("BatteryCenter not found")
}
guard let c = NSClassFromString("BCBatteryDeviceController") as? NSObjectProtocol else {
fatalError("BCBatteryDeviceController not found")
}
let instance = c.performSelector("sharedInstance").takeUnretainedValue()
if let devices = instance.valueForKey("connectedDevices") as? [AnyObject] {
// You will have more than one battery in connectedDevices if your device is using a Smart Case
for battery in devices {
print(battery)
}
}
// Simulator
// <BCBatteryDevice: 0x7fc208d23930;**FAKE DEVICE** vendor = Apple; productIdentifier = 0; parts = (null); identifier = 1; matchIdentifier = (null); baseIdentifier = InternalBattery-0; name = iPad; groupName =InternalBattery-0; percentCharge = 100; lowBattery = NO; connected = YES; charging = NO; internal = YES; powerSource = YES; poweredSoureState = Unknown; transportType = 1 >
// iPhone 6
// BCBatteryDevice: 0x15764a3d0; vendor = Apple; productIdentifier = 0; parts = (null); matchIdentifier = (null); baseIdentifier = InternalBattery-0; name = iPhone; percentCharge = 63; lowBattery = NO; connected = YES; charging = YES; internal = YES; powerSource = YES; poweredSoureState = AC Power; transportType = 1 >
@RSBOMB
Copy link

RSBOMB commented Mar 15, 2019

Kindly give the demo how to use it.

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