2

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?

Eimantas
  • 48,927
  • 17
  • 132
  • 168
Rony
  • 1,229
  • 4
  • 22
  • 42
  • 2
    I think it's the job of Apple to do it for you. AFAIK, you cann't access it programmatically. – Mahesh Jan 13 '11 at 07:12
  • Apple gives me alert.....but after getting this type of alert i need to pause my game.....e.g: hot pursuit – Rony Jan 13 '11 at 07:27

1 Answers1

5

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

Pang
  • 9,564
  • 146
  • 81
  • 122
Sorin Antohi
  • 6,145
  • 9
  • 45
  • 71
  • according to your code should i have to run a infinite schedule and always check the battery level??...i am actually expecting some delegate..... – Rony Jan 13 '11 at 08:27
  • 1
    i didn't see any protocol for this. but you can create an NSTimer with a good interval that will call a method that will update the value.if you see that the value goes below a critical level throw a notification. Your game could listen for that notification and pause the game...just an idea. – Sorin Antohi Jan 13 '11 at 11:06
  • is that the only way? many game(e.g hot pursuit,ice age) pause the app when battery power is below 20%......are they check battery status all time and do it?? – Rony May 26 '11 at 09:03
  • You don't need a timer, battery state and level are observable. See https://stackoverflow.com/questions/14834506/detect-low-battery-warning-ios – lazarevzubov Oct 16 '21 at 19:01