#import"ViewController.h"
創(chuàng)新互聯(lián)公司主要從事成都做網(wǎng)站、網(wǎng)站設計、網(wǎng)頁設計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務盤龍,十余年網(wǎng)站建設經驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:028-86922220
#import
@interfaceViewController ()
//@property(nonatomic,readonly) float batteryLevel NS_AVAILABLE_IOS(3_0);
// 0 .. 1.0. -1.0 if UIDeviceBatteryStateUnknown它返回的是0.00-1.00之間的浮點值。
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
//打開電池的監(jiān)聽
[UIDevicecurrentDevice].batteryMonitoringEnabled =YES;
//獲取電池的狀態(tài)
UIDeviceBatteryState BatteryState = [UIDevicecurrentDevice].batteryState;
//獲取剩余電量范圍在0.000000至 1.000000之間
CGFloat batterylevel = [UIDevicecurrentDevice].batteryLevel;
//battery的狀態(tài)分為:
/*
UIDeviceBatteryStateUnknown, 未知
UIDeviceBatteryStateUnplugged, //未充電
UIDeviceBatteryStateCharging, //正在充電
UIDeviceBatteryStateFull, //滿電
*/
if (BatteryState ==UIDeviceBatteryStateUnknown) {
self.dlLabel.text = [NSStringstringWithFormat:@"%0.0f%%",batterylevel*100];
NSLog(@"unknow");
}else{
NSLog(@"know");
//將剩余的電量用label顯示。
self.dlLabel.text = [NSStringstringWithFormat:@"%0.0f%%",batterylevel*100];
}
//
// [self getCurrentBatteryLevel];
// [UIDevice currentDevice].batteryMonitoringEnabled = YES;
// [[NSNotificationCenter defaultCenter]
// addObserverForName:UIDeviceBatteryLevelDidChangeNotification
// object:nil queue:[NSOperationQueue mainQueue]
// usingBlock:^(NSNotification *notification) {
// // Level has changed
// dl=[UIDevice currentDevice].batteryLevel;
// NSLog(@"電池電量:%.2f", [UIDevice currentDevice].batteryLevel);
//
// self.dlLabel.text = [NSString stringWithFormat:@"%f",[UIDevice currentDevice].batteryLevel];
//
// }];
// Do any additional setup after loading the view, typically from a nib.
}