2015-10-27 84 views
1

我正在嘗試在iOS 9.1下使用BatteryCenter和​​私有框架並藉助nst's iOS Runtime Headers。這是出於研究目的,並不會讓它進入AppStore。私有iOS框架返回NULL

這裏有各自的代碼:

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

錯過了什麼,我的代碼沒有返回什麼期望?它是否需要權利?如果是這樣的話?

回答

0

執行成功塊後,您需要先訪問BCBatteryDeviceController,通過該塊可以獲取所有連接設備的列表。

以下是相同的代碼。

Class CommonUtilities = NSClassFromString(@"BCBatteryDeviceController"); 

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

BCBatteryDeviceController* objBCBatteryDeviceController = si; 

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

在斯威夫特,我設法得到這個工作沒有BatteryCenter標題。我仍然在尋找一種方式來訪問連接電池的列表,而無需使用BCBatteryDeviceController,但是這是我迄今爲止的工作:

斯威夫特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) 
    } 
} 

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