4

I'm making a turnbased game. To prevent users from cheat, the turn will automatically get passed to the opponent if the user close the app in the middle of a turn. This because so the user can't close the app, restart it, and beging the turn from the beginning.

There are two cases that should penalize the player however. If a phone call gets in, or the low battery warning appears. I can detect the phone call coming in and respond, but I don't know what to do with the battery?

Any suggestions would be awesome

BlackMouse
  • 4,442
  • 6
  • 38
  • 65

1 Answers1

13

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

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

iPhone OS 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];

ALso refer this link.

iPatel
  • 46,010
  • 16
  • 115
  • 137
  • I dont understand the "device" object on your notification receiver. Your reffered link using "nil" object, and I'm also think is the right one. – Ryde Nov 25 '15 at 03:07
  • Even though the information in the answer is true, it doesn't answer the question. Observing battery level and battery state doesn't let you know when your app is interrupted with the low-battery alert. – lazarevzubov Oct 16 '21 at 18:50