How can I get battery level and state (plugged in, discharging, charging, etc) in iOS?
We need to write below code for monitoring the battery
UIDevice.current.isBatteryMonitoringEnabled = true
|
After that we retrieve the battery state by using this method
var batteryState: UIDevice.BatteryState {
return UIDevice.current.batteryState
}
|
And write switch cases for those batterysate and we can get the state.
func updateBatteryStateLabel() {
switch batteryState {
case .charging:
status = "Charging"
case .unknown:
status = "Unknown"
case .unplugged:
status = "Unplugged"
case .full:
status = "Full"
}
DispatchQueue.main.async {
self.batteryStsLbl.text = "B: \(self.status)"
}
}
|