透過UIDevice類別可以得到裝置資訊,
例如裝置名稱、作業系統版本以及電池狀態,
若要得知電池狀態必須先打開電池狀態監視的開罐。
程式碼如下:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[UIDevice currentDevice].batteryMonitoringEnabled = YES;
NSLog(@"Battery level: %.0f%%", [UIDevice currentDevice].batteryLevel * 100 );
switch ([UIDevice currentDevice].batteryState) {
case UIDeviceBatteryStateUnknown:
NSLog(@"unknow device");
break;
case UIDeviceBatteryStateUnplugged:
NSLog(@"unplugged");
case UIDeviceBatteryStateCharging:
NSLog(@"chargeing");
case UIDeviceBatteryStateFull:
NSLog(@"full");
default:
break;
}
}
@end
執行結果如下:
2015-08-10 23:24:22.589 battryStatus[16070:7418803] Battery level: 100%
2015-08-10 23:24:22.589 battryStatus[16070:7418803] chargeing
2015-08-10 23:24:22.590 battryStatus[16070:7418803] full
文章標籤
全站熱搜
留言列表