iPhone: Get warning when battery power is very low

前端 未结 1 550
借酒劲吻你
借酒劲吻你 2021-01-07 02:28

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条回答
  •  长发绾君心
    2021-01-07 03:23

    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

    0 讨论(0)
提交回复
热议问题