Skip to content

Instantly share code, notes, and snippets.

@RSBOMB
Forked from JALsnipe/BatteryInfo.swift
Created March 14, 2019 09:14
Show Gist options
  • Save RSBOMB/88e0c6214d20a9020298e3c4bbdc3c58 to your computer and use it in GitHub Desktop.
Save RSBOMB/88e0c6214d20a9020298e3c4bbdc3c58 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 >
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment