我正在尝试在 iOS 9.1 下使用 BatteryCenter
和 CommonUtilities
私有(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/