iOS 獲取設備信息

有兩種方法,一種是使用 UIDevice,另外一種是使用 Linux 獲取硬件信息的方式。

方法一

UIDevice* uiDevice = [UIDevice currentDevice];  
NSBundle* nsBundle = [NSBundle mainBundle];
NSDictionary *infoDictionary = [nsBundle infoDictionary];
NSString* crashInfo = [NSString stringWithFormat:@"Identifier:%@\nVersion:%@\nOS Version:%@     %@\nDate/Time:%@\nHardware Model:%@",
                        [nsBundle bundleIdentifier],
                        [infoDictionary objectForKey:@"CFBundleVersion"],
                        [uiDevice systemName],
                        [uiDevice systemVersion],
                        [self GetOnlyTime],
                        [self deviceString] ];

方法二

#import "sys/utsname.h"

struct utsname systemInfo;
uname(&systemInfo);

NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]; 

最新代碼詳見Github

方法三

if NSProcessInfo().isOperatingSystemAtLeastVersion(NSOperatingSystemVersion(majorVersion: 9, minorVersion: 0, patchVersion: 0)) {
    println("iOS >= 9.0.0")
}

let os = NSProcessInfo().operatingSystemVersion
switch (os.majorVersion, os.minorVersion, os.patchVersion) {
    case (8, 0, _):
        println("iOS >= 8.0.0, < 8.1.0")
    case (8, _, _):
        println("iOS >= 8.1.0, < 9.0")
    case (9, _, _):
        println("iOS >= 9.0.0")
    default:
        // this code will have already crashed on iOS 7, so >= iOS 10.0
        println("iOS >= 10.0.0")
}

參考資料

蘋果硬件參數對照表

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章