Swift - 获取应用名称、应用版本、设备型号、系统版本等信息

前端之家收集整理的这篇文章主要介绍了Swift - 获取应用名称、应用版本、设备型号、系统版本等信息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有时我们在 App中提交一些统计信息或者用户反馈信息时,为了能更好地进行分析,通常会附带上当前应用程序的名称、版本号、设备型号、以及设备系统版本。下面演示如何获取这些信息。

1,效果

程序启动后自动获取相关的应用信息以及设备信息,并打印到控制台中。

2,样例代码

默认情况下我从 UIDevice获取设备型号时只能得到 iPhoneiPod Touch这样笼统的类型数据。
这里对 UIDevice做个扩展,使其可以得到具体的设备型号,比如: iPhone 6s Plus这样准确的设备款式。
@H_301_49@ 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()
//应用程序信息
let infoDictionary = Bundle .main.infoDictionary!
appDisplayName = infoDictionary[ "CFBundleDisplayName" ] //程序名称
majorVersion = infoDictionary[ "CFBundleShortVersionString" //主程序版本号
minorVersion = infoDictionary[ "CFBundleVersion" //版本号(内部标示)
appVersion = majorVersion as ! String
//设备信息
iosVersion = UIDevice .current.systemVersion //iOS版本
identifierNumber = UIDevice .current.identifierForVendor //设备udid
systemName = .current.systemName //设备名称
model = .current.model //设备型号
modelName = .current.modelName //设备具体型号
localizedModel = .current.localizedModel //设备区域化型号如A1533
//打印信息
print ( "程序名称:\(appDisplayName)" )
"主程序版本号:\(appVersion)" )
"内部版本号:\(minorVersion)" )
"iOS版本:\(iosVersion)" )
"设备udid:\(identifierNumber)" )
"设备名称:\(systemName)" )
"设备型号:\(model)" )
"设备具体型号:\(modelName)" )
"设备区域化型号:\(localizedModel)" )
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
}
//扩展UIDevice
extension {
//获取设备具体详细的型号
var modelName: String {
systemInfo = utsname()
uname(&systemInfo)
machineMirror = Mirror (reflecting: systemInfo.machine)
identifier = machineMirror.children.reduce( "" ) { identifier,element in
guard value = element.value ? Int8 ,value != 0 else { return identifier }
identifier + ( UnicodeScalar ( UInt8 (value)))
}
switch identifier {
case "iPod5,1" : return "iPod Touch 5"
"iPod7,monospace!important; min-height:auto!important; color:blue!important; background:none!important">"iPod Touch 6"
"iPhone3, "iPhone 4"
"iPhone4,monospace!important; min-height:auto!important; color:blue!important; background:none!important">"iPhone 4s"
"iPhone5,monospace!important; min-height:auto!important; color:blue!important; background:none!important">"iPhone 5"
"iPhone 5c"
"iPhone6,monospace!important; min-height:auto!important; color:blue!important; background:none!important">"iPhone 5s"
"iPhone7,monospace!important; min-height:auto!important; color:blue!important; background:none!important">"iPhone 6"
"iPhone 6 Plus"
"iPhone8,monospace!important; min-height:auto!important; color:blue!important; background:none!important">"iPhone 6s"
"iPhone 6s Plus"
"iPhone9,monospace!important; min-height:auto!important; color:blue!important; background:none!important">"iPhone 7 (CDMA)"
"iPhone 7 (GSM)"
"iPhone 7 Plus (CDMA)"
"iPhone 7 Plus (GSM)"
"iPad2,monospace!important; min-height:auto!important; color:blue!important; background:none!important">"iPad 2"
"iPad3,monospace!important; min-height:auto!important; color:blue!important; background:none!important">"iPad 3"
"iPad 4"
"iPad4,monospace!important; min-height:auto!important; color:blue!important; background:none!important">"iPad Air"
"iPad5,monospace!important; min-height:auto!important; color:blue!important; background:none!important">"iPad Air 2"
"iPad Mini"
"iPad Mini 2"
"iPad Mini 3"
"iPad Mini 4"
"iPad6,monospace!important; min-height:auto!important; color:blue!important; background:none!important">"iPad Pro"
"AppleTV5,monospace!important; min-height:auto!important; color:blue!important; background:none!important">"Apple TV"
"i386" "x86_64" "Simulator"
default identifier
}
}
}

原文出自: www.hangge.com 转载请保留原文链接 http://www.hangge.com/blog/cache/detail_1606.html

猜你在找的Swift相关文章