是否可以以編程方式閱讀手機電池的百分比從iOS應用程序?iOS:是否可以通過編程讀取手機電池的百分比?
2
A
回答
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)")
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDevice_Class/#//apple_ref/occ/instp/UIDevice/batteryLevel – Paulw11