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?
Xcode 11 • Swift 5.1 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.
Review your iPhone battery healthGo to Settings > Battery. Tap Battery Health. iPhone displays information about your battery's capacity, peak performance, and whether your battery needs to be serviced.
Check battery life & useOpen your phone's Settings app. Under "Battery," see how much charge you have left, and about how long it will last. For details, tap Battery.
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") } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With