B3TA B3TA - 1 month ago 33
iOS Question

Check Battery Level iOS Swift

I just started Swift and I have been looking for a way to check the battery level. I found this resource and have been playing around with it but for some reason can't seem to get it to work.

I wasn't quite sure how to go about fixing this. Any ideas?


First just enable battery monitoring:

UIDevice.currentDevice().batteryMonitoringEnabled = true

Then you can create a computed property to return the battery level:

var batteryLevel: Float {
    return UIDevice.currentDevice().batteryLevel

Xcode 8 GM • Swift 3

UIDevice.current.isBatteryMonitoringEnabled = true

var batteryLevel: Float {
    return UIDevice.current.batteryLevel