透過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

 

arrow
arrow
    文章標籤
    iOS 取得電池狀態 UIDevice
    全站熱搜
    創作者介紹
    創作者 Will 的頭像
    Will

    Will的部落格

    Will 發表在 痞客邦 留言(0) 人氣()