Does anyone know how battery state notification (UIDevice.batteryStateDidChangeNotification
) is supposed to work regarding app foreground/background state?
Assume there is no other reason why the app is running in the background. I have enabled UIDevice.current.isBatteryMonitoringEnabled
when the app was in the foreground. What should happen if the external power is later connected or removed when the app is in the background? The docs don't mention this.
Possibilities include
- I don't get a notification, so I should check the state myself when the app next comes to the foreground.
- I'll get a notification when the app next comes to the foreground, if the state changed while it was in the background.
- The app will be woken up in the background to receive the notification.
- The app will be kept running in the background while
isBatteryMonitoringEnabled
is true.
It looks as if it's doing either 3 or 4, which I find a bit surprising. But is this influenced by the fact that it's connected (wirelessly) to the debugger?
Hi,
When you run your app from the debugger your app cannot be terminated since the debugger attached to it and keeps it alive.
Your app will not be woken up by battery notifications but you can use things like location updates, push notifications and background fetch to periodically get this information since these APIs will wake your app in the background (at least when the device battery is not low). Some APIs like APNs may not deliver if the battery is low, as an example.
Hopefully this helps.
Rico
WWDR | DTS | Software Engineer