iPhone: Get warning when battery power is very low

笑着哭i 提交于 2019-12-19 04:14:32

问题


I want to know how can i get warning in my app delegate when the device battery power is very low. So that i can pause the running game.

Any idea?


回答1:


You could use the battery level property from UIDevice. If the battery level is less than 5% show an alert for example. You could poll periodically for the battery level in your app delegate for example.

UIDevice *myDevice = [UIDevice currentDevice]; 
[myDevice setBatteryMonitoringEnabled:YES]; 
float batteryLevel = [myDevice batteryLevel];

Explanation from the docs:

batteryLevel
The battery charge level for the device. (read-only)

@property(nonatomic, readonly) float batteryLevel

Discussion
Battery level ranges from 0.0 (fully discharged) to 1.0 (100% charged). Before accessing this property, ensure that battery monitoring is enabled.

If battery monitoring is not enabled, battery state is UIDeviceBatteryStateUnknown and the value of this property is –1.0.

Availability
Available in iOS 3.0 and later.

See Also
@property batteryState
@property batteryMonitoringEnabled

Declared In
UIDevice.h



来源:https://stackoverflow.com/questions/4677515/iphone-get-warning-when-battery-power-is-very-low

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!