iOS獲取設備型號和App版本號等信息(OC+Swift)

iOS獲取設備型號和App版本號等信息(OC+Swift)
字數1687 閱讀382 評論3 喜歡10
很久沒有寫過博客了,由於中間工做比較忙,而後有些我的事情因此耽誤了。可是以前寫的博客還一直有人來問一些問題,有些以前寫的已經比較久了,有些須要實時更新的也沒來得及更新,發現會跟一些童鞋形成困惑,因此之後也但願能及時更新和寫一些新的內容。app

我以前博客的地址:一毛的博客,也是主要介紹iOS開發的,之後都在簡書更新吧。但願你們能繼續支持。
iOS開發過程當中,有時候爲了更好的用戶體驗或者爲了bug跟蹤,可能會須要獲取用戶的應用信息、系統信息、設備信息。這些信息的獲取能夠根據不一樣的設備或者App、系統版原本提供不一樣的功能或更好的用戶體驗,或者讓開發者能更好的分析用戶的問題緣由。ide

你說不要囉嗦了:Talk is cheap, show me the code!ui

好的,code來了:code

獲取設備名稱element

OC代碼
NSString *deviceName = [[UIDevice currentDevice] name];開發

Swift代碼
let deviceName = UIDevice.currentDevice().name博客

獲取系統版本號string

OC代碼
NSString *sysVersion = [[UIDevice currentDevice] systemVersion];it

Swift代碼
let sysVersion = UIDevice.currentDevice().systemVersionio

獲取設備惟一標識符

OC代碼
NSString *deviceUUID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];

Swift代碼
let deviceUUID = UIDevice.currentDevice().identifierForVendor?.UUIDString

獲取設備的型號

OC代碼
NSString *deviceModel = [[UIDevice currentDevice] model];

Swift代碼
let deviceModel = UIDevice.currentDevice().model

獲取App相關的信息

OC代碼

NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];

// 獲取App的版本號
NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];

// 獲取App的build版本
NSString *appBuildVersion = [infoDic objectForKey:@"CFBundleVersion"];

// 獲取App的名稱
NSString *appName = [infoDic objectForKey:@"CFBundleDisplayName"];
Swift代碼

let infoDic = NSBundle.mainBundle().infoDictionary

// 獲取App的版本號
let appVersion = infoDic?["CFBundleShortVersionString"]

// 獲取App的build版本
let appBuildVersion = infoDic?["CFBundleVersion"]

// 獲取App的名稱
let appName = infoDic?["CFBundleDisplayName"]
有些人可能會說了:「褲子都脫了,你就給我看這個!看你以前寫的博客還能看點兒乾貨,今天第一篇就寫着麼個玩意兒?尤爲是獲取設備型號那個,獲取出來一個iPhone,你肯定不是在逗我吧?」

看官別急,其實此次最主要的就是來分享獲取設備型號的方法的,由於我看國內還不多有Swift寫的一些東西,也不知道是否是由於我沒有找到,就想本身寫出來給你們分享,順便作個筆記。

你會說:獲取設備型號比較麻煩,又用的是C語言的一些東西,比較麻煩,並且還得記住全部設備版本號例如:iPhone8,2,實在是記不住啊。固然不用記住,用的時候拷過來就好了,由於我也記不住😄

iOS獲取具體設備型號
OC代碼獲取iOS設備型號:
// 須要#import <sys/utsname.h>

  • (NSString)deviceModelName
    {
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString
    deviceModel = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];

    //iPhone 系列
    if ([deviceModel isEqualToString:@"iPhone1,1"]) return @"iPhone 1G";
    if ([deviceModel isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
    if ([deviceModel isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
    if ([deviceModel isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
    if ([deviceModel isEqualToString:@"iPhone3,2"]) return @"Verizon iPhone 4";
    if ([deviceModel isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
    if ([deviceModel isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
    if ([deviceModel isEqualToString:@"iPhone5,2"]) return @"iPhone 5";
    if ([deviceModel isEqualToString:@"iPhone5,3"]) return @"iPhone 5C";
    if ([deviceModel isEqualToString:@"iPhone5,4"]) return @"iPhone 5C";
    if ([deviceModel isEqualToString:@"iPhone6,1"]) return @"iPhone 5S";
    if ([deviceModel isEqualToString:@"iPhone6,2"]) return @"iPhone 5S";
    if ([deviceModel isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
    if ([deviceModel isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
    if ([deviceModel isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";
    if ([deviceModel isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";

    //iPod 系列
    if ([deviceModel isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
    if ([deviceModel isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
    if ([deviceModel isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
    if ([deviceModel isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G";
    if ([deviceModel isEqualToString:@"iPod5,1"]) return @"iPod Touch 5G";

    //iPad 系列
    if ([deviceModel isEqualToString:@"iPad1,1"]) return @"iPad";
    if ([deviceModel isEqualToString:@"iPad2,1"]) return @"iPad 2 (WiFi)";
    if ([deviceModel isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)";
    if ([deviceModel isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)";
    if ([deviceModel isEqualToString:@"iPad2,4"]) return @"iPad 2 (32nm)";
    if ([deviceModel isEqualToString:@"iPad2,5"]) return @"iPad mini (WiFi)";
    if ([deviceModel isEqualToString:@"iPad2,6"]) return @"iPad mini (GSM)";
    if ([deviceModel isEqualToString:@"iPad2,7"]) return @"iPad mini (CDMA)";

    if ([deviceModel isEqualToString:@"iPad3,1"]) return @"iPad 3(WiFi)";
    if ([deviceModel isEqualToString:@"iPad3,2"]) return @"iPad 3(CDMA)";
    if ([deviceModel isEqualToString:@"iPad3,3"]) return @"iPad 3(4G)";
    if ([deviceModel isEqualToString:@"iPad3,4"]) return @"iPad 4 (WiFi)";
    if ([deviceModel isEqualToString:@"iPad3,5"]) return @"iPad 4 (4G)";
    if ([deviceModel isEqualToString:@"iPad3,6"]) return @"iPad 4 (CDMA)";

    if ([deviceModel isEqualToString:@"iPad4,1"]) return @"iPad Air";
    if ([deviceModel isEqualToString:@"iPad4,2"]) return @"iPad Air";
    if ([deviceModel isEqualToString:@"iPad4,3"]) return @"iPad Air";
    if ([deviceModel isEqualToString:@"iPad5,3"]) return @"iPad Air 2";
    if ([deviceModel isEqualToString:@"iPad5,4"]) return @"iPad Air 2";
    if ([deviceModel isEqualToString:@"i386"]) return @"Simulator";
    if ([deviceModel isEqualToString:@"x86_64"]) return @"Simulator";

    if ([deviceModel isEqualToString:@"iPad4,4"]
    ||[deviceModel isEqualToString:@"iPad4,5"]
    ||[deviceModel isEqualToString:@"iPad4,6"]) return @"iPad mini 2";

    if ([deviceModel isEqualToString:@"iPad4,7"]
    ||[deviceModel isEqualToString:@"iPad4,8"]
    ||[deviceModel isEqualToString:@"iPad4,9"]) return @"iPad mini 3";

    return deviceModel;
    }
    記得必定要#import <sys/utsname.h>啊,到時候沒有導入編譯不經過了不要找我啊。

Swift代碼獲取iOS設備型號:
//MARK: - UIDevice延展
public extension UIDevice {

var modelName: String {
    var systemInfo = utsname()
    uname(&systemInfo)
    let machineMirror = Mirror(reflecting: systemInfo.machine)
    let identifier = machineMirror.children.reduce("") { identifier, element in
        guard let value = element.value as? Int8 where value != 0 else { return identifier }
        return identifier + String(UnicodeScalar(UInt8(value)))
    }

    switch identifier {
    case "iPod5,1":                                 return "iPod Touch 5"
    case "iPod7,1":                                 return "iPod Touch 6"
    case "iPhone3,1", "iPhone3,2", "iPhone3,3":     return "iPhone 4"
    case "iPhone4,1":                               return "iPhone 4s"
    case "iPhone5,1", "iPhone5,2":                  return "iPhone 5"
    case "iPhone5,3", "iPhone5,4":                  return "iPhone 5c"
    case "iPhone6,1", "iPhone6,2":                  return "iPhone 5s"
    case "iPhone7,2":                               return "iPhone 6"
    case "iPhone7,1":                               return "iPhone 6 Plus"
    case "iPhone8,1":                               return "iPhone 6s"
    case "iPhone8,2":                               return "iPhone 6s Plus"
    case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4":return "iPad 2"
    case "iPad3,1", "iPad3,2", "iPad3,3":           return "iPad 3"
    case "iPad3,4", "iPad3,5", "iPad3,6":           return "iPad 4"
    case "iPad4,1", "iPad4,2", "iPad4,3":           return "iPad Air"
    case "iPad5,3", "iPad5,4":                      return "iPad Air 2"
    case "iPad2,5", "iPad2,6", "iPad2,7":           return "iPad Mini"
    case "iPad4,4", "iPad4,5", "iPad4,6":           return "iPad Mini 2"
    case "iPad4,7", "iPad4,8", "iPad4,9":           return "iPad Mini 3"
    case "iPad5,1", "iPad5,2":                      return "iPad Mini 4"
    case "iPad6,7", "iPad6,8":                      return "iPad Pro"
    case "AppleTV5,3":                              return "Apple TV"
    case "i386", "x86_64":                          return "Simulator"
    default:                                        return identifier
    }
}

}
這個是在 Swift 2.0 版本以後才能使用的:如今不會還有人使用 Swift 1.2 吧?

你還問我怎麼用?好吧,騷年,送佛送到西,您看好了,把上面的擴展扔到你的項目中,而後就能夠用下面的語句調用了:

let modelName = UIDevice.currentDevice().modelName

好了,今天就寫到這兒吧,但願之後還有時間有精力來更多的更新一些內容吧。

相關文章
相關標籤/搜索