iOS獲取設備信息

jopen 9年前發布 | 5K 次閱讀 Objective-C IOS

獲取iOS設備信息需要用到UIDevice類,UIDevice.h文件定義了這些屬性:

    @property(nonatomic,readonly,retain) NSString    name;              // e.g. "My iPhone"
@property(nonatomic,readonly,retain) NSString
model; // e.g. @"iPhone", @"iPod touch"
@property(nonatomic,readonly,retain) NSString localizedModel; // localized version of model
@property(nonatomic,readonly,retain) NSString
systemName; // e.g. @"iOS"
@property(nonatomic,readonly,retain) NSString *systemVersion; // e.g. @"4.0"
@property(nonatomic,readonly) UIDeviceOrientation orientation; // return current device orientation. this will return UIDeviceOrientationUnknown unless device orientation notifications are being generated.

@property(nonatomic,readonly,retain) NSUUID      *identifierForVendor NS_AVAILABLE_IOS(6_0);      // a UUID that may be used to uniquely identify the device, same across apps from a single vendor.  

@property(nonatomic,readonly,getter=isGeneratingDeviceOrientationNotifications) BOOL generatesDeviceOrientationNotifications;  

@property(nonatomic,getter=isBatteryMonitoringEnabled) BOOL batteryMonitoringEnabled NS_AVAILABLE_IOS(3_0);  // default is NO  
@property(nonatomic,readonly) UIDeviceBatteryState          batteryState NS_AVAILABLE_IOS(3_0);  // UIDeviceBatteryStateUnknown if monitoring disabled  
@property(nonatomic,readonly) float                         batteryLevel NS_AVAILABLE_IOS(3_0);  // 0 .. 1.0. -1.0 if UIDeviceBatteryStateUnknown  

@property(nonatomic,getter=isProximityMonitoringEnabled) BOOL proximityMonitoringEnabled NS_AVAILABLE_IOS(3_0); // default is NO  
@property(nonatomic,readonly)                            BOOL proximityState NS_AVAILABLE_IOS(3_0);  // always returns NO if no proximity detector  

@property(nonatomic,readonly,getter=isMultitaskingSupported) BOOL multitaskingSupported NS_AVAILABLE_IOS(4_0);  

@property(nonatomic,readonly) UIUserInterfaceIdiom userInterfaceIdiom NS_AVAILABLE_IOS(3_2);  </pre> 


下面通過簡單例子介紹這些屬性!

環境:Xcode 6.1,iOS 8.1

設備:iPhone 4s(iOS 7.1.1)

代碼如下:

    // 獲取設備信息

- (void) getDevInfo  
{  
    UIDevice* myDevice = [UIDevice currentDevice];  
    NSLog(@"設備名稱 - %@", myDevice.name);  
    NSLog(@"設備模式 - %@", myDevice.model);  
    NSLog(@"設備本地模式 - %@", myDevice.localizedModel);  
    NSLog(@"系統名稱 - %@", myDevice.systemName);  
    NSLog(@"系統版本 - %@", myDevice.systemVersion);  

//    typedef NS_ENUM(NSInteger, UIDeviceOrientation) {  
//        UIDeviceOrientationUnknown,  
//        UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom  
//        UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top  
//        UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right  
//        UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left  
//        UIDeviceOrientationFaceUp,              // Device oriented flat, face up  
//        UIDeviceOrientationFaceDown             // Device oriented flat, face down  
//    };  
    NSLog(@"設備朝向 - %d", myDevice.orientation);  // 詳見UIDeviceOrientation  

    NSLog(@"設備UUID - %@", myDevice.identifierForVendor);  

    // 當前設備是否有轉向通知  
    NSLog(@"設備轉向通知(BOOL) - %hhd", myDevice.generatesDeviceOrientationNotifications);  

    // 是否啟動電池監控,默認為NO  
    NSLog(@"電池監控啟用狀態(BOOL) - %hhd", myDevice.batteryMonitoringEnabled);  

//    typedef NS_ENUM(NSInteger, UIDeviceBatteryState) {  
//        UIDeviceBatteryStateUnknown,  
//        UIDeviceBatteryStateUnplugged,   // on battery, discharging  
//        UIDeviceBatteryStateCharging,    // plugged in, less than 100%  
//        UIDeviceBatteryStateFull,        // plugged in, at 100%  
//    };              // available in iPhone 3.0  
    NSLog(@"電池狀態 - %d", myDevice.batteryState);  // 詳見UIDeviceBatteryState  

    // 電量百分比, 0到1.0,如果電池狀態為UIDeviceBatteryStateUnknown,則百分比為-1.0  
    NSLog(@"電池電量 - %f", myDevice.batteryLevel);  

    // 是否啟動接近監控(比如臉接近手機屏),默認為NO  
    NSLog(@"接近監控啟用狀態(BOOL) - %hhd", myDevice.proximityMonitoringEnabled);  

    // 如果沒有接近感應器,則返回NO  
    NSLog(@"接近狀態(BOOL) - %hhd", myDevice.proximityState);  

    NSLog(@"是否支持多任務(BOOL) - %hhd", myDevice.multitaskingSupported);  

//    typedef NS_ENUM(NSInteger, UIUserInterfaceIdiom) {  
//        UIUserInterfaceIdiomUnspecified = -1,  
//#if __IPHONE_3_2 <= __IPHONE_OS_VERSION_MAX_ALLOWED  
//        UIUserInterfaceIdiomPhone,           // iPhone and iPod touch style UI  
//        UIUserInterfaceIdiomPad,             // iPad style UI  
//#endif  
//    };  
    NSLog(@"用戶界面模式 - %d", myDevice.userInterfaceIdiom);  // 詳見UIUserInterfaceIdiom  
}  </pre> 


打印結果:

    2015-01-29 11:35:23.294 TestProject[261:60b] 設備名稱 - iPhone  
    2015-01-29 11:35:23.297 TestProject[261:60b] 設備模式 - iPhone  
    2015-01-29 11:35:23.298 TestProject[261:60b] 設備本地模式 - iPhone  
    2015-01-29 11:35:23.300 TestProject[261:60b] 系統名稱 - iPhone OS  
    2015-01-29 11:35:23.302 TestProject[261:60b] 系統版本 - 7.1.1  
    2015-01-29 11:35:23.303 TestProject[261:60b] 設備朝向 - 0  
    2015-01-29 11:35:23.311 TestProject[261:60b] 設備UUID - <__NSConcreteUUID 0x16565a90> 7737D97A-35F6-4C0E-B0EC-DF1D711D99E1  
    2015-01-29 11:35:23.313 TestProject[261:60b] 設備轉向通知(BOOL) - 0  
    2015-01-29 11:35:23.314 TestProject[261:60b] 電池監控啟用狀態(BOOL) - 0  
    2015-01-29 11:35:23.316 TestProject[261:60b] 電池狀態 - 0  
    2015-01-29 11:35:23.317 TestProject[261:60b] 電池電量 - -1.000000  
    2015-01-29 11:35:23.319 TestProject[261:60b] 接近監控啟用狀態(BOOL) - 0  
    2015-01-29 11:35:23.321 TestProject[261:60b] 接近狀態(BOOL) - 0  
    2015-01-29 11:35:23.322 TestProject[261:60b] 是否支持多任務(BOOL) - 1  
    2015-01-29 11:35:23.324 TestProject[261:60b] 用戶界面模式 - 0  

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!