Can
Can

Reputation: 4726

Private iOS Framework Returning NULL

I'm trying to use BatteryCenter and CommonUtilities private frameworks under iOS 9.1 with the help of nst's iOS Runtime Headers. It's for research purposes and won't make it to the AppStore.

Here are their respective codes:

- (void)batteryCenter {
NSBundle *bundle = [NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/BatteryCenter.framework"];
BOOL success = [bundle load];

    if(success) {
        Class BCBatteryDevice = NSClassFromString(@"BCBatteryDevice");
        id si = [[BCBatteryDevice alloc] init];

        NSLog(@"Charging: %@", [si valueForKey:@"charging"]);
    }
}


- (void)commonUtilities {
    NSBundle *bundle = [NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/CommonUtilities.framework"];
    BOOL success = [bundle load];

    if(success) {
        Class CommonUtilities = NSClassFromString(@"CUTWiFiManager");
        id si = [CommonUtilities valueForKey:@"sharedInstance"];

        NSLog(@"Is Wi-Fi Enabled: %@", [si valueForKey:@"isWiFiEnabled"]);
        NSLog(@"Wi-Fi Scaled RSSI: %@", [si valueForKey:@"wiFiScaledRSSI"]);
        NSLog(@"Wi-Fi Scaled RSSI: %@", [si valueForKey:@"lastWiFiPowerInfo"]);
    }
}

Although I get the classes back, all of their respected values are NULL which is weird since some must be true, e.g. I'm connected to Wi-Fi so isWiFiEnabled should be YES.

What exactly is missing that my code doesn't return whats expected? Does it need entitlement(s)? If so what exactly?

Upvotes: 2

Views: 718

Answers (2)

JAL
JAL

Reputation: 42449

In Swift, I managed to get this working without the BatteryCenter headers. I'm still looking for a way to access the list of attached batteries without using BCBatteryDeviceController, but this is what I have working so far:

Swift 3:

guard case let batteryCenterHandle = dlopen("/System/Library/PrivateFrameworks/BatteryCenter.framework/BatteryCenter", RTLD_LAZY), batteryCenterHandle != nil else {
    fatalError("BatteryCenter not found")
}

guard let batteryDeviceControllerClass = NSClassFromString("BCBatteryDeviceController") as? NSObjectProtocol else {
    fatalError("BCBatteryDeviceController not found")
}

let instance = batteryDeviceControllerClass.perform(Selector(("sharedInstance"))).takeUnretainedValue()

if let devices = instance.value(forKey: "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)
    }
}

Swift 2.2:

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)
    }
}

This logs:

<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 >

Upvotes: 2

Jas_meet
Jas_meet

Reputation: 366

You need to first access the BCBatteryDeviceController, after success block is executed, through which you can get list of all the connected devices.

Here is the code for the same.

Class CommonUtilities = NSClassFromString(@"BCBatteryDeviceController");

id si = [CommonUtilities valueForKey:@"sharedInstance"];

BCBatteryDeviceController* objBCBatteryDeviceController = si;

NSLog(@"Connected devices: %@", objBCBatteryDeviceController.connectedDevices);

Upvotes: 0

Related Questions