您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

UIDevice 實戰:取得系統版本、電量狀態與 proximity 感測(真機實測)

編輯:iOS 開發綜合

UIDevice 是 iOS 取得裝置資訊的統一入口,從系統名稱、版本、電池狀態到方向感測一應俱全。以下整理成一份可直接複製使用的範例,註解已標明各欄位的含義與注意事項。以下程式碼經真機實測可用。

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 作業系統名稱
    NSString *osName = [[UIDevice currentDevice] systemName];

    // 作業系統版本
    NSString *systemVersion = [[UIDevice currentDevice] systemVersion];

    NSLog(@"os = %@", osName);
    NSLog(@"version = %@", systemVersion);

    // 裝置家族(iPhone / iPod / iPad)
    NSString *iosmodel = [[UIDevice currentDevice] model];
    NSLog(@"%@", iosmodel);

    // 電量:範圍 0.0(完全耗盡)~ 1.0(100%)
    // 讀取前必須先確認 batteryMonitoringEnabled 已開啟
    float batteryLevel = [UIDevice currentDevice].batteryLevel;
    NSLog(@"%f", batteryLevel);

    // 電池狀態
    UIDeviceBatteryState batteryState = [[UIDevice currentDevice] batteryState];
    // UIDeviceBatteryStateUnknown    = 0  無法識別
    // UIDeviceBatteryStateUnplugged  = 1  使用電池(未插電)
    // UIDeviceBatteryStateCharging   = 2  充電中,尚未滿
    // UIDeviceBatteryStateFull       = 3  已充滿
    NSLog(@"%d", batteryState);

    // 是否支援多工
    BOOL support = [[UIDevice currentDevice] isMultitaskingSupported];
    if (support) {
        NSLog(@"support multiTask");
    } else {
        NSLog(@"don't support multiTask");
    }

    // 是否已啟用裝置方向通知
    // YES:方向改變會被回報;NO:不會
    BOOL status = [UIDevice currentDevice].generatesDeviceOrientationNotifications;
    NSLog(@"%d", status);

    // 開始回報方向變化;若需要在方向改變時做事,可監聽
    // UIDeviceOrientationDidChangeNotification
    // [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

    // 停止回報方向變化
    // [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

    // 臨近感測(proximity)
    // 當身體靠近 iPhone 而非觸碰螢幕時,系統會給出反應
    // 一般需要距離約 5mm 以內、且有一定面積的遮擋才會觸發
    // YES:已靠近,觸發通話時熄屏等行為
    BOOL proximityState = [[UIDevice currentDevice] proximityState];
    NSLog(@"%d", proximityState);

    UIDevice *device = [UIDevice currentDevice];
    device.proximityMonitoringEnabled = YES;   // 允許臨近檢測

    // 訂閱臨近狀態變化
    [[NSNotificationCenter defaultCenter] addObserver:self
                                            selector:@selector(proximityChanged:)
                                                name:UIDeviceProximityStateDidChangeNotification
                                              object:device];
}

// 臨近狀態變化的回呼
- (void)proximityChanged:(NSNotification *)notification {
    UIDevice *device = [notification object];
    NSLog(@"In proximity: %i", device.proximityState);
    if (device.proximityState == 1) {
        // 使用者已靠近,在這裡處理業務邏輯
    }
}

幾個容易踩到的細節

電量要先開監控。不設定 UIDevice.currentDevice.batteryMonitoringEnabled = YES 就直接讀 batteryLevel,在部分情境下會拿到 -1 這個無效值。這個開關預設是關閉的,因為持續監控會带来額外耗電。

UIDeviceBatteryStateUnplugged 的語意容易誤解。它代表「正在使用電池」,也就是拔掉了電源線,而不是「充電中」;充電中對應的是 Charging。這個命名在早期文件裡造成過不少混淆。

方向通知要成對使用。beginGeneratingDeviceOrientationNotifications 與 endGeneratingDeviceOrientationNotifications 是計數配對的,begin 幾次就要 end 幾次,否則系統會一直維持那套感測硬體的供電。

proximity 監控同樣要記得關閉。用完之後把 proximityMonitoringEnabled 設回 NO,並在 dealloc 中移除通知觀察者,避免殘留的註冊造成非預期行為。

這些 API 大多只能在真機上驗證——模擬器的電池狀態與 proximity 值都不具參考性,測試時請務必接上實機。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved