Q:

iOS Battery monitoring Swift

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?

A:

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

