2011-09-09 4 views
1

이것은 간단한 배터리 레벨 테스트 앱의 간단한 코드입니다.정확한 배터리 레벨을 얻는 방법은 무엇입니까?

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UIDevice *device = [UIDevice currentDevice]; 
    device.batteryMonitoringEnabled = YES; 
    mLabel.text = [NSString stringWithFormat:@"%0.5f",device.batteryLevel]; 
} 

이것은 내 응용 프로그램의 이미지입니다. 나는 왜이 수준이 다른지 모르겠다. ....? 한 99 % 수준이며, 실제로는 95 %

enter image description here

답변

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

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

옵저버 방법되는 0.95 앱

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