-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathUIDeviceExtension.swift
More file actions
266 lines (223 loc) · 8.04 KB
/
Copy pathUIDeviceExtension.swift
File metadata and controls
266 lines (223 loc) · 8.04 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//
// UIDeviceExtension.swift
// Device
//
// Created by Johannes Schickling on 7/20/15.
//
//
// MARK: Imports
import Foundation
import UIKit
// MARK: -
/// Enum representing the different types of iOS devices available
public enum DeviceType: String, CaseIterable {
case iPhone2G
case iPhone3G
case iPhone3GS
case iPhone4
case iPhone4S
case iPhone5
case iPhone5C
case iPhone5S
case iPhone6
case iPhone6Plus
case iPhone6S
case iPhone6SPlus
case iPhoneSE
case iPhone7
case iPhone7Plus
case iPhone8
case iPhone8Plus
case iPhoneX
case iPhoneXS
case iPhoneXSMax
case iPhoneXR
case iPhone11
case iPhone11Pro
case iPhone11ProMax
case iPhoneSEGen2
case iPhone12Mini
case iPhone12
case iPhone12Pro
case iPhone12ProMax
case iPodTouch1G
case iPodTouch2G
case iPodTouch3G
case iPodTouch4G
case iPodTouch5G
case iPodTouch6G
case iPodTouch7G
case iPad
case iPad2
case iPad3
case iPad4
case iPad5
case iPad6
case iPadMini
case iPadMiniRetina
case iPadMini3
case iPadMini4
case iPadAir
case iPadAir2
case iPadPro9Inch
case iPadPro10p5Inch
case iPadPro11Inch
case iPadPro12Inch
case simulator
case notAvailable
// MARK: Constants
/// The current device type
public static var current: DeviceType {
var systemInfo = utsname()
uname(&systemInfo)
let machine = systemInfo.machine
let mirror = Mirror(reflecting: machine)
var identifier = ""
for child in mirror.children {
if let value = child.value as? Int8, value != 0 {
identifier.append(String(UnicodeScalar(UInt8(value))))
}
}
return DeviceType(identifier: identifier)
}
// MARK: Variables
/// The display name of the device type
public var displayName: String {
switch self {
case .iPhone2G: return "iPhone 2G"
case .iPhone3G: return "iPhone 3G"
case .iPhone3GS: return "iPhone 3GS"
case .iPhone4: return "iPhone 4"
case .iPhone4S: return "iPhone 4S"
case .iPhone5: return "iPhone 5"
case .iPhone5C: return "iPhone 5C"
case .iPhone5S: return "iPhone 5S"
case .iPhone6Plus: return "iPhone 6 Plus"
case .iPhone6: return "iPhone 6"
case .iPhone6S: return "iPhone 6S"
case .iPhone6SPlus: return "iPhone 6S Plus"
case .iPhoneSE: return "iPhone SE"
case .iPhone7: return "iPhone 7"
case .iPhone7Plus: return "iPhone 7 Plus"
case .iPhone8: return "iPhone 8"
case .iPhone8Plus: return "iPhone 8 Plus"
case .iPhoneX: return "iPhone X"
case .iPhoneXS: return "iPhone XS"
case .iPhoneXSMax: return "iPhone XS Max"
case .iPhoneXR: return "iPhone XR"
case .iPhone11: return "iPhone 11"
case .iPhone11Pro: return "iPhone 11 Pro"
case .iPhone11ProMax: return "iPhone 11 Pro Max"
case .iPhoneSEGen2: return "iPhone SE (2nd Gen)"
case .iPhone12Mini: return "iPhone 12 Mini"
case .iPhone12: return "iPhone 12"
case .iPhone12Pro: return "iPhone 12 Pro"
case .iPhone12ProMax: return "iPhone 12 Pro Max"
case .iPodTouch1G: return "iPod Touch 1G"
case .iPodTouch2G: return "iPod Touch 2G"
case .iPodTouch3G: return "iPod Touch 3G"
case .iPodTouch4G: return "iPod Touch 4G"
case .iPodTouch5G: return "iPod Touch 5G"
case .iPodTouch6G: return "iPod Touch 6G"
case .iPodTouch7G: return "iPod Touch 7G"
case .iPad: return "iPad"
case .iPad2: return "iPad 2"
case .iPad3: return "iPad 3"
case .iPad4: return "iPad 4"
case .iPad5: return "iPad 5"
case .iPad6: return "iPad 6"
case .iPadMini: return "iPad Mini"
case .iPadMiniRetina: return "iPad Mini Retina"
case .iPadMini3: return "iPad Mini 3"
case .iPadMini4: return "iPad Mini 4"
case .iPadAir: return "iPad Air"
case .iPadAir2: return "iPad Air 2"
case .iPadPro9Inch: return "iPad Pro 9 Inch"
case .iPadPro10p5Inch: return "iPad Pro 10.5 Inch"
case .iPadPro11Inch: return "iPad Pro 11 Inch"
case .iPadPro12Inch: return "iPad Pro 12 Inch"
case .simulator: return "Simulator"
case .notAvailable: return "Not Available"
}
}
/// The identifiers associated with each device type
internal var identifiers: [String] {
switch self {
case .notAvailable: return []
case .simulator: return ["i386", "x86_64"]
case .iPhone2G: return ["iPhone1,1"]
case .iPhone3G: return ["iPhone1,2"]
case .iPhone3GS: return ["iPhone2,1"]
case .iPhone4: return ["iPhone3,1", "iPhone3,2", "iPhone3,3"]
case .iPhone4S: return ["iPhone4,1"]
case .iPhone5: return ["iPhone5,1", "iPhone5,2"]
case .iPhone5C: return ["iPhone5,3", "iPhone5,4"]
case .iPhone5S: return ["iPhone6,1", "iPhone6,2"]
case .iPhone6: return ["iPhone7,2"]
case .iPhone6Plus: return ["iPhone7,1"]
case .iPhone6S: return ["iPhone8,1"]
case .iPhone6SPlus: return ["iPhone8,2"]
case .iPhoneSE: return ["iPhone8,4"]
case .iPhone7: return ["iPhone9,1", "iPhone9,3"]
case .iPhone7Plus: return ["iPhone9,2", "iPhone9,4"]
case .iPhone8: return ["iPhone10,1", "iPhone10,4"]
case .iPhone8Plus: return ["iPhone10,2", "iPhone10,5"]
case .iPhoneX: return ["iPhone10,3", "iPhone10,6"]
case .iPhoneXS: return ["iPhone11,2"]
case .iPhoneXSMax: return ["iPhone11,4", "iPhone11,6"]
case .iPhoneXR: return ["iPhone11,8"]
case .iPhone11: return ["iPhone12,1"]
case .iPhone11Pro: return ["iPhone12,3"]
case .iPhone11ProMax: return ["iPhone12,5"]
case .iPhoneSEGen2: return ["iPhone12,8"]
case .iPhone12Mini: return ["iPhone13,1"]
case .iPhone12: return ["iPhone13,2"]
case .iPhone12Pro: return ["iPhone13,3"]
case .iPhone12ProMax: return ["iPhone13,4"]
case .iPodTouch1G: return ["iPod1,1"]
case .iPodTouch2G: return ["iPod2,1"]
case .iPodTouch3G: return ["iPod3,1"]
case .iPodTouch4G: return ["iPod4,1"]
case .iPodTouch5G: return ["iPod5,1"]
case .iPodTouch6G: return ["iPod7,1"]
case .iPodTouch7G: return ["iPod9,1"]
case .iPad: return ["iPad1,1", "iPad1,2"]
case .iPad2: return ["iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4"]
case .iPad3: return ["iPad3,1", "iPad3,2", "iPad3,3"]
case .iPad4: return ["iPad3,4", "iPad3,5", "iPad3,6"]
case .iPad5: return ["iPad6,11", "iPad6,12"]
case .iPad6: return ["iPad7,5", "iPad7,6"]
case .iPadMini: return ["iPad2,5", "iPad2,6", "iPad2,7"]
case .iPadMiniRetina: return ["iPad4,4", "iPad4,5", "iPad4,6"]
case .iPadMini3: return ["iPad4,7", "iPad4,8", "iPad4,9"]
case .iPadMini4: return ["iPad5,1", "iPad5,2"]
case .iPadAir: return ["iPad4,1", "iPad4,2", "iPad4,3"]
case .iPadAir2: return ["iPad5,3", "iPad5,4"]
case .iPadPro9Inch: return ["iPad6,3", "iPad6,4"]
case .iPadPro10p5Inch: return ["iPad7,3", "iPad7,4"]
case .iPadPro11Inch: return ["iPad8,1", "iPad8,2", "iPad8,3", "iPad8,4"]
case .iPadPro12Inch: return ["iPad6,7", "iPad6,8", "iPad7,1", "iPad7,2", "iPad8,5", "iPad8,6", "iPad8,7", "iPad8,8"]
}
}
// MARK: Inits
/// Creates a device type
///
/// - Parameter identifier: The identifier of the device
internal init(identifier: String) {
for device in DeviceType.allCases {
for deviceId in device.identifiers {
guard identifier == deviceId else { continue }
self = device
return
}
}
self = .notAvailable
}
}
// MARK: -
public extension UIDevice {
/// The `DeviceType` of the device in use
public var deviceType: DeviceType {
return DeviceType.current
}
}