Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何获得准确的电池电量?_Ios_Swift_Battery - Fatal编程技术网

Ios 如何获得准确的电池电量?

Ios 如何获得准确的电池电量?,ios,swift,battery,Ios,Swift,Battery,这是我的简单batteryLevel测试应用程序的简单代码 - (void)viewDidLoad { [super viewDidLoad]; UIDevice *device = [UIDevice currentDevice]; device.batteryMonitoringEnabled = YES; mLabel.text = [NSString stringWithFormat:@"%0.5f",device.batteryLevel]; } 这是

这是我的简单batteryLevel测试应用程序的简单代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIDevice *device = [UIDevice currentDevice];
    device.batteryMonitoringEnabled = YES;
    mLabel.text = [NSString stringWithFormat:@"%0.5f",device.batteryLevel];
}
这是我的应用程序的图像,希望你能看到状态栏电池电量和我的应用程序电池电量。我不知道为什么这些级别不同。。。。? 一个是99%,应用程序级别是0.95,实际上是95%

// Enable this property
UIDevice.current.isBatteryMonitoringEnabled = true

// Then add observer
NotificationCenter.default.addObserver(self, selector: #selector(batteryLevelDidChange(notification:)), name: NSNotification.Name.UIDeviceBatteryLevelDidChange, object: nil)

观察者方法

@objc func batteryLevelDidChange(notification: NSNotification) {
  // The battery's level did change
  print(UIDevice.current.batteryLevel)
}
观察者方法

@objc func batteryLevelDidChange(notification: NSNotification) {
  // The battery's level did change
  print(UIDevice.current.batteryLevel)
}