Hi I am new in IOS development and I am using swift. I want to get battery level of my device programmatically. But always I receive -1.0
Below is my code -
import UIKit
class FirstController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
UIDevice.current.isBatteryMonitoringEnabled = true
}
var batteyLevel : Float {
return UIDevice.current.batteryLevel
}
// attached with a touchInsideUp event
@IBAction func onButtonClick(_ sender: Any) {
// print "Battery moniter true"
print("Battery moniter \(UIDevice.current.isBatteryMonitoringEnabled)")
// print "Battery -1.0"
print("Battery \(batteyLevel)")
}
}
Please help how to find battery level