iOS:是否可以通过编程方式读取 phone 电池百分比?
iOS: is it possible to programmatically read the phone battery percentage?
是否可以通过编程方式读取来自iOS应用程序的phone电池百分比?
您可以使用下面的代码。
if (![[UIDevice currentDevice] isBatteryMonitoringEnabled])
{
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
}
NSLog(@"battery : %f", [[UIDevice currentDevice] batteryLevel]);
You can you this code to check battery state and life
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);
Swift版本。
if !(UIDevice.currentDevice.isBatteryMonitoringEnabled){
UIDevice.currentDevice.isBatteryMonitoringEnabled = true
}
print("Battery level: \(UIDevice.currentDevice.batteryLevel)")
是否可以通过编程方式读取来自iOS应用程序的phone电池百分比?
您可以使用下面的代码。
if (![[UIDevice currentDevice] isBatteryMonitoringEnabled])
{
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
}
NSLog(@"battery : %f", [[UIDevice currentDevice] batteryLevel]);
You can you this code to check battery state and life
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);
Swift版本。
if !(UIDevice.currentDevice.isBatteryMonitoringEnabled){
UIDevice.currentDevice.isBatteryMonitoringEnabled = true
}
print("Battery level: \(UIDevice.currentDevice.batteryLevel)")