Swift - 获取应用名称、应用版本、设备型号、系统版本等信息
生活随笔
收集整理的這篇文章主要介紹了
Swift - 获取应用名称、应用版本、设备型号、系统版本等信息
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
有時(shí)我們?cè)?
App?
中提交一些統(tǒng)計(jì)信息或者用戶反饋信息時(shí),為了能更好地進(jìn)行分析,通常會(huì)附帶上當(dāng)前應(yīng)用程序的名稱、版本號(hào)、設(shè)備型號(hào)、以及設(shè)備系統(tǒng)版本。下面演示如何獲取這些信息。
1,效果圖
程序啟動(dòng)后自動(dòng)獲取相關(guān)的應(yīng)用信息以及設(shè)備信息,并打印到控制臺(tái)中。2,樣例代碼
默認(rèn)情況下我從?UIDevice?中獲取設(shè)備型號(hào)時(shí)只能得到?iPhone、iPod Touch?這樣籠統(tǒng)的類型數(shù)據(jù)。 這里對(duì)?UIDevice?做個(gè)擴(kuò)展,使其可以得到具體的設(shè)備型號(hào),比如:iPhone 6s Plus?這樣準(zhǔn)確的設(shè)備款式。| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | import?UIKit class?ViewController:?UIViewController?{ ????override?func?viewDidLoad() { ????????super.viewDidLoad() ????????? ????????//應(yīng)用程序信息 ????????let?infoDictionary =?Bundle.main.infoDictionary! ????????let?appDisplayName = infoDictionary["CFBundleDisplayName"]?//程序名稱 ????????let?majorVersion = infoDictionary["CFBundleShortVersionString"]//主程序版本號(hào) ????????let?minorVersion = infoDictionary["CFBundleVersion"]//版本號(hào)(內(nèi)部標(biāo)示) ????????let?appVersion = majorVersion?as!?String ????????? ????????//設(shè)備信息 ????????let?iosVersion =?UIDevice.current.systemVersion?//iOS版本 ????????let?identifierNumber =?UIDevice.current.identifierForVendor?//設(shè)備udid ????????let?systemName =?UIDevice.current.systemName?//設(shè)備名稱 ????????let?model =?UIDevice.current.model?//設(shè)備型號(hào) ????????let?modelName =?UIDevice.current.modelName?//設(shè)備具體型號(hào) ????????let?localizedModel =?UIDevice.current.localizedModel?//設(shè)備區(qū)域化型號(hào)如A1533 ????????? ????????//打印信息 ????????print("程序名稱:\(appDisplayName)") ????????print("主程序版本號(hào):\(appVersion)") ????????print("內(nèi)部版本號(hào):\(minorVersion)") ????????print("iOS版本:\(iosVersion)") ????????print("設(shè)備udid:\(identifierNumber)") ????????print("設(shè)備名稱:\(systemName)") ????????print("設(shè)備型號(hào):\(model)") ????????print("設(shè)備具體型號(hào):\(modelName)") ????????print("設(shè)備區(qū)域化型號(hào):\(localizedModel)") ????} ????override?func?didReceiveMemoryWarning() { ????????super.didReceiveMemoryWarning() ????} } //擴(kuò)展UIDevice extension?UIDevice?{ ????//獲取設(shè)備具體詳細(xì)的型號(hào) ????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, 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?"iPhone9,1":???????????????????????????????return?"iPhone 7 (CDMA)" ????????case?"iPhone9,3":???????????????????????????????return?"iPhone 7 (GSM)" ????????case?"iPhone9,2":???????????????????????????????return?"iPhone 7 Plus (CDMA)" ????????case?"iPhone9,4":???????????????????????????????return?"iPhone 7 Plus (GSM)" ????????????? ????????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 ????????} ????} } |
總結(jié)
以上是生活随笔為你收集整理的Swift - 获取应用名称、应用版本、设备型号、系统版本等信息的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 纳米珠磨机可以运用在哪些领域
- 下一篇: Windows XP SP3 无需密匙即