2012-08-03 76 views
1

我试图正确获取电池电量。但是这个价值并不像iPhone的状态栏中的价值。iOS:如何正确获取电池电量

通过代码使用的UIDevice:

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES]; 

[[UIDevice currentDevice] batteryLevel]; 

请帮助我的人!我需要正确的电池电量,就像在状态栏imdimatery。

+3

您是否真的阅读过文档? http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/occ/instp/UIDevice/batteryLevel – Luke 2012-08-03 19:19:21

+0

可能的dumplicate与http://stackoverflow.com/questions/11807295/how-to-get-real-time-battery-level-on-ios – cat 2013-05-28 02:56:56

回答

10

井苹果文档说此:

电池水平从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; 

希望这有助于!

+0

:(我做到了,但是在搜索步骤中返回5%。100% - > 95% - > 90%。 。但是那不正确地喜欢状态栏中的值,请帮助我另一种方式。我在ios5中使用了一些软件,它们得到正确的值。我认为我们可以得到...但我不怎么做。坏 – lithewall 2012-08-04 02:56:00

+1

你不能没有越狱你不能*确切*电池百分比 – Sirens 2013-03-05 01:09:05

+0

我知道它的旧,但有一个名为电池医生的应用程序商店这个应用程序,它显示的电池电量和充电状态的确切值有关它如何做的任何想法? – ShayanK 2016-03-15 22:50:53