Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check Battery Level iOS Swift [closed]

Tags:

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?

like image 662
B3TA Avatar asked Dec 14 '14 23:12

B3TA


People also ask

How do I show battery percentage in Swift?

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.

How do I check battery life on iOS?

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.

How do you check how long your battery has left?

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.


1 Answers

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")     } } 
like image 89
Leo Dabus Avatar answered Sep 25 '22 02:09

Leo Dabus