sivag1
sivag1

Reputation: 5004

iOS Battery monitoring Swift

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

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

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

Upvotes: 8

Views: 3465

Answers (3)

Andrey Seredkin
Andrey Seredkin

Reputation: 681

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.

Upvotes: 3

Karthick A S
Karthick A S

Reputation: 61

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

Upvotes: 6

user250282
user250282

Reputation: 51

I don't know what's the problem but try this: Instead of using batterylevel, use

UIDevice.currentDevice().batteryState

Kindly provide your full code for apt answer.

Upvotes: -1

Related Questions