ios - 私有(private) iOS 框架返回 NULL

标签 ios iphone-privateapi

我正在尝试在 iOS 9.1 下使用 BatteryCenterCommonUtilities 私有(private)框架 nst's iOS Runtime Headers 。它仅用于研究目的,不会出现在 AppStore 上。

以下是它们各自的代码:

- (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"]);
    }
}

虽然我拿回了类,但它们所有受尊重的值都是 NULL,这很奇怪,因为有些必须是真的,例如我已连接到 Wi-Fi,因此 isWiFiEnabled 应该为 YES

我的代码到底缺少什么,没有返回预期的结果?它需要权利吗?如果是的话具体是什么?

最佳答案

在 Swift 中,我设法在没有 BatteryCenter header 的情况下实现此功能。我仍在寻找一种无需使用 BCBatteryDeviceController 即可访问所连接电池列表的方法,但这就是我迄今为止所做的工作:

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

此日志:

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

关于ios - 私有(private) iOS 框架返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33371513/

相关文章:

iphone - 从应用程序密码退出

ios - 在我的表格 View 底部添加一个工具栏

ios - iOS 8 是否支持动态链接?

ios - 将基于 UINavigationController 的应用程序转换为基于 UIDocumentBrowser 的应用程序

ios - 检查应用程序是否有使用私有(private)框架的通知?

ios - Apple 在提交应用程序时警告我的应用程序正在使用非公共(public)选择器

iOS MobileSubstrate CFNotificationCenterPostNotification - 不工作

IOS coredata 神奇的记录 stupAutoMigratingCoreDataStack 不起作用

ios - 在运行时更新 UITabBarController 项目标题

ios - 一种使用私有(private) API 重启 iOS 设备或重启 Springboard 的方法?