35

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?

AstroCB
  • 12,337
  • 20
  • 57
  • 73
B3TA
  • 469
  • 1
  • 5
  • 8

2 Answers2

147

Xcode 11 • Swift 5.1

First just enable battery monitoring:

UIDevice.current.isBatteryMonitoringEnabled = true

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

Battery level ranges from 0.0 (fully discharged) to 1.0 (100% charged). Before accessing this property, ensure that battery monitoring is enabled. If battery monitoring is not enabled, battery state is UIDevice.BatteryState.unknown and the value of this property is –1.0.

var batteryLevel: Float { UIDevice.current.batteryLevel }

To monitor your device battery level you can add an observer for the UIDevice.batteryLevelDidChangeNotification:

NotificationCenter.default.addObserver(self, selector: #selector(batteryLevelDidChange), name: UIDevice.batteryLevelDidChangeNotification, object: nil)

Battery level ranges from 0.0 (fully discharged) to 1.0 (100% charged). Before accessing this property, ensure that battery monitoring is enabled.
If battery monitoring is not enabled, battery state is UIDevice.BatteryState.unknown and the value of this property is –1.0.


@objc func batteryLevelDidChange(_ notification: Notification) {
    print(batteryLevel)
}

You can also verify the battery state:

var batteryState: UIDevice.BatteryState { UIDevice.current.batteryState }

case .unknown   //  "The battery state for the device cannot be determined."
case .unplugged //  "The device is not plugged into power; the battery is discharging"
case .charging  //  "The device is plugged into power and the battery is less than 100% charged."
case .full      //   "The device is plugged into power and the battery is 100% charged."

and add an observer for UIDevice.batteryStateDidChangeNotification:

NotificationCenter.default.addObserver(self, selector: #selector(batteryStateDidChange), name: UIDevice.batteryStateDidChangeNotification, object: nil)

@objc func batteryStateDidChange(_ notification: Notification) {
    switch batteryState {
    case .unplugged, .unknown:
        print("not charging")
    case .charging, .full:
        print("charging or full")
    }
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • 1
    Superb Answer..!! – Yash Bedi Sep 08 '17 at 05:41
  • 4
    Not working in my case. Receiving -1.0 always – Sanjay Kumar Jan 22 '18 at 14:42
  • 1
    Swift Syntax changed a bit for Notifications: NotificationCenter.default.addObserver(self, selector: #selector(batteryLevelDidChange), name: UIDevice.batteryLevelDidChangeNotification, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(batteryStateDidChange), name: UIDevice.batteryStateDidChangeNotification, object: nil) – Repose Oct 25 '18 at 13:59
-2

var batteryLevel: Float { get }

Make sure that battery monitoring is enabled first =)

Apple Documentation