iOS:如何正确获取电池电量

我试图得到正确的电池电量。 但是这个值不像iphone的状态栏中的值。

通过代码使用UIDevice:

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES]; [[UIDevice currentDevice] batteryLevel]; 

请有人帮我! 我需要得到正确的电池级别,就像在状态栏imdimatery。

那么苹果公司的文件这样说:

电池电量范围从0.0(完全放电)到1.0(100%充电)。 在访问此属性之前,请确保电池监控已启用。

所以你的代码应该是这样的:

 [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES]; float batteryLevel = [[UIDevice currentDevice] batteryLevel]; //This will give you the battery between 0.0 (empty) and 1.0 (100% charged) //If you want it as a percentage, you can do this: batteryLevel *= 100; 

希望这可以帮助!