嗨,我是IOS开发的新手,我正在使用swift。我想以编程方式获取设备的电池电量。但总是收到-1.0
以下是我的代码 -
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)")
}
}
请帮助查找电池电量