B3TA B3TA - 1 year ago 610
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?

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download