iPhone: Get warning when battery power is very low
I 开发者_C百科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?
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
精彩评论