38

I am working on a mobile game, which appearantly crashes when the Low Battery alert is displayed. It works fine on low memory, incoming calls and other messages.

Its a pain to test and debug this, since I can find no terminal or iPhone simulator way of simulating this situation, so I have to charge my phone up a little bit, launch the app, wait for it to drain its power, and start all over again.

Does anyone know of a way to produce this error in a realistic way? Hopefully something that isn't too stressful on my iPhone battery.

Nils Munch
  • 8,805
  • 11
  • 51
  • 103
  • good question. I've had a similar issue – Paul de Lange Jun 14 '12 at 08:41
  • When does it crash? When you get "low battery alerts" or something else? – Bo. Jun 14 '12 at 08:41
  • Just when the blue alert shows up, the app crashes. The battery warning stays up. – Nils Munch Jun 14 '12 at 08:43
  • Does your app crash if any other system alerts show? For example Cellular Data warnings, etc, or is it literally just the low power alert? –  Jun 19 '12 at 11:59
  • Nothing like that no, it doesn't crash on incoming sms, call or cell data warnings... sadly. :P – Nils Munch Jun 19 '12 at 12:04
  • How did you fix it ? my app has the same issue. I am more looking for solution than the simulation. any code snippet would help. Thanks is advance :-) – mask Dec 18 '12 at 21:11

4 Answers4

15

Unfortunately, there is no good way to simulate a low-battery environment. You actually will most likely need to physically charge your device's battery until it is just above the "low battery" state and then debug your application.

To address what others have said:

  1. There is no way to simulate low battery notifications. The project that @Bo. provided does nothing more than schedule random UILocalNotifications. It isn't all that much different than just showing a UIAlertView in your app.
  2. You could try what @Andrew R. said and use the private methods found in the UIDevice header. But it is doubtful that you will exactly mimic the effects of a real low-battery environment.

Although it is a pain to have to wait for you device to actually hit the low-battery state, you could add some battery-draining code to your app to assist you. For example, using the GPS might drain the battery a bit quicker.

Good luck.

Community
  • 1
  • 1
Michael Frederick
  • 16,664
  • 3
  • 43
  • 58
1

Did you try simulating the low battery notifications? There seems to be a project that does that: https://github.com/MegaEduX/LowBattery

Nils Munch
  • 8,805
  • 11
  • 51
  • 103
Bo.
  • 2,547
  • 3
  • 24
  • 36
  • Sadly this doesnt really seem to make a realistic output for me... It just seems to mimic the warning, and doesn't make my app crash, and this still occurs on a "real" iphone running low on power. – Nils Munch Jun 14 '12 at 13:43
-2

Assuming this is for testing purposes only, you could give the following private UIDevice methods a try:

-(void)_setBatteryState:(int)state;
-(void)_setBatteryLevel:(float)level;

You'll have to experiment to see what parameters they expect, or whether they have an impact at all.

Andrew R.
  • 923
  • 2
  • 8
  • 14
  • 2
    while the `_setBatteryLevel` function is available via category, and while in that cateogory, calling `[self performSelector:@selector(_setBatteryLevel:) withObject:[NSNumber numberWithFloat:0.01]]` will cause the value returned by `[[UIDevice currentDevice] batteryLevel]` to return the value you sent, it won't trigger a low-memory warning. – john.k.doe Jun 24 '12 at 01:41
  • @john.k.doe have you try setting it to 25, 20? I heard low battery warning is triggered at 5% interval and warning is only shown when it reaches level limit – onmyway133 May 07 '13 at 04:51
-3

In iOS there is way to simulate "Low Battery"

Battery monitoring is enabled by setting to YES a property of the UIDevice singleton:

UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES;

iOS provides two type of battery monitoring events, one for when the state changes (e.g., charging, unplugged, full charged) and one that updates when the battery’s charge level changes. As was the case with proximity monitoring, you register callbacks to receive notifications:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged:) name:@"UIDeviceBatteryLevelDidChangeNotification" object:device];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged:) name:@"UIDeviceBatteryStateDidChangeNotification" object:device];
iBhavik
  • 663
  • 11
  • 28