2016-08-05 84 views

回答

2

您可以使用下面的代碼。

if (![[UIDevice currentDevice] isBatteryMonitoringEnabled]) 
{ 
    [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES]; 
} 
NSLog(@"battery : %f", [[UIDevice currentDevice] batteryLevel]); 
0

您可以將這些代碼來檢查電池狀態和生活

UIDevice *device = [UIDevice currentDevice]; 
    [device setBatteryMonitoringEnabled:YES]; 

    int state = [device batteryState]; 
    // 0 - UnKnown 1 - UnPlugged 2 - Charging 3 - Full 
    NSLog(@"Battery Current State : %d",state);  

    double batteryPer = (float) [device batteryLevel] * 100; 
    NSLog(@"Battery Percentage Left: %ld", batteryPer); 
0

斯威夫特版本。

if !(UIDevice.currentDevice.isBatteryMonitoringEnabled){ 
    UIDevice.currentDevice.isBatteryMonitoringEnabled = true 
} 
print("Battery level: \(UIDevice.currentDevice.batteryLevel)")