iOS Battery monitoring Swift

Viewed 2354

I have set the monitoring enabled, but still the battery level is coming as -1 in both Emulator and device.

UIDevice.currentDevice().batteryMonitoringEnabled = true
var level = UIDevice.currentDevice().batteryLevel

The level variable is always -1 for the emulator and most of the times -1 for physical device. What else should I do to the value working?

3 Answers

If battery monitoring is not enabled, battery state is UIDevice.BatteryState.unknown and the value of this property is –1.0. Ensure that you have enabled isBatteryMonitoringEnabled properly.

Swift 4

UIDevice.current.isBatteryMonitoringEnabled = true
let batteryLevel = UIDevice.current.batteryLevel

Well, it seems that batteryMonitoringEnabled doesn't work on Simulators. It simply doesn't change when I set it to true. So state becomes UIDeviceBatteryStateUnknown then. Found no related notes in Apple's docs.

Related