是否可以通过编程方式读取来自iOS应用的手机电池百分比?
答案 0 :(得分:2)
您可以使用以下代码。
if (![[UIDevice currentDevice] isBatteryMonitoringEnabled])
{
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
}
NSLog(@"battery : %f", [[UIDevice currentDevice] batteryLevel]);
答案 1 :(得分: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);
答案 2 :(得分:0)
Swift版本。
if !(UIDevice.currentDevice.isBatteryMonitoringEnabled){
UIDevice.currentDevice.isBatteryMonitoringEnabled = true
}
print("Battery level: \(UIDevice.currentDevice.batteryLevel)")