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 { And write switch cases for those batterysate and we can get the state. func updateBatteryStateLabel() {
return UIDevice.current.batteryState
}
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)"
}
}
How to take a screenshot on iOS programmatically ?
How to make a phone call in iOS 10 using Swift?
How to prevent the screen from sleeping in iOS Programmatically?
How do i get build/version number of an iOS App?
How can I get battery level and state (plugged in, discharging, charging, etc) in iOS?