-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeviceDetector
305 lines (283 loc) · 13.5 KB
/
DeviceDetector
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
extension UIDevice {
private func getIdentifier() -> String?{
#if targetEnvironment(simulator)
let identifier = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"]!
return identifier
#else
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)))
}
#endif
return ""
}
enum DeviceType:String {
case iPhone35 = "iPhone 3,5"
case iPhone40 = "iPhone 4"
case iPhone47 = "iPhone 6, iPhone 6S, iPhone 7, iPhone 8"
case iPhone55 = "iPhone 6Plus, iPhone 6S Plus, iPhone 7Plus, iPhone 8 Plus"
case iPhone58 = "iPhone X, iPhone Xs"
case iPhone61_65 = "iPhoneXsMax, iPhoneXR"
case iPad79 = "iPad 7,9"
case iPad97 = "iPad 9,7"
case iPad105 = "iPad 10,5"
case iPad11 = "iPad 11"
case iPad129 = "iPad 12,9"
case iPad = "iPad"
case TV = "Apple TV"
case carPlay = "CarPlay"
var isPhone: Bool {
return [.iPhone35, .iPhone40, .iPhone47, .iPhone55, .iPhone58, .iPhone61_65].contains(self)
}
var isIPad: Bool {
return [.iPad79, .iPad97, .iPad105, .iPad11, .iPad129].contains(self)
}
}
enum DeviceModel {
case simulator
case iPod1
case iPod2
case iPod3
case iPod4
case iPod5
case iPod6
case iPad1
case iPad2
case iPad3
case iPad4
case iPad5
case iPad6
case iPadAir1
case iPadAir2
case iPadPro9_7
case iPadPro10_5
case iPadPro11
case iPadPro12_9
case iPadPro12_9_2nd
case iPadPro12_9_3rd
case iPadMini1
case iPadMini2
case iPadMini3
case iPadMini4
case iPhone
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 iPhoneXR
case iPhoneXS
case iPhoneXSMax
case airPods
case appleTV2
case appleTV3
case appleTV4
case appleTV4K
case appleWatch
case appleWatchS1
case appleWatchS2
case appleWatchS3
case appleWatchS4
case carPlay
}
var deviceType:DeviceType? {
switch UIDevice.current.userInterfaceIdiom {
case .tv:
return .TV
case .pad:
let screen = UIScreen.main.bounds.size
let height = max(screen.width, screen.height)
let identifier = self.getIdentifier()
switch height {
case 1024:
switch identifier{
case "iPad2,5", "iPad2,6", "iPad2,7", "iPad4,4", "iPad4,5", "iPad4,6", "iPad4,7", "iPad4,8", "iPad4,9","iPad5,1", "iPad5,2":
return .iPad79
case "iPad1,1", "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4", "iPad3,1", "iPad3,2", "iPad3,3", "iPad3,4", "iPad3,5", "iPad3,6", "iPad6,11", "iPad6,12", "iPad7,5", "iPad7,6", "iPad4,1", "iPad4,2", "iPad4,3", "iPad5,3", "iPad5,4", "iPad6,3", "iPad6,4":
return .iPad97
default:
return .iPad
}
case 1112:
switch identifier {
case "iPad7,3", "iPad7,4":
return .iPad105
default:
return .iPad
}
case 1194:
return .iPad11
case 1366:
return .iPad129
default:
return nil
}
case .phone:
let screen = UIScreen.main.bounds.size
let height = max(screen.width, screen.height)
switch height {
case 480:
return .iPhone35
case 568:
return .iPhone40
case 667:
return .iPhone47
case 736:
return .iPhone55
case 812:
return .iPhone58
case 896:
return .iPhone61_65
default:
return nil
}
case .carPlay:
return .carPlay
case .unspecified:
return nil
}
}
var deviceModel:DeviceModel? {
#if targetEnvironment(simulator)
let identifier = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"]!
#else
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)))
}
#endif
switch identifier {
case "iPad1,1": return .iPad1
case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4": return .iPad2
case "iPad3,1", "iPad3,2", "iPad3,3": return .iPad3
case "iPad3,4", "iPad3,5", "iPad3,6": return .iPad4
case "iPad6,11", "iPad6,12": return .iPad5
case "iPad7,5", "iPad7,6": return .iPad6
case "iPad4,1", "iPad4,2", "iPad4,3": return .iPadAir1
case "iPad5,3", "iPad5,4": return .iPadAir2
case "iPad6,3", "iPad6,4": return .iPadPro9_7
case "iPad7,3", "iPad7,4": return .iPadPro10_5
case "iPad8,1", "iPad8,2", "iPad8,3", "iPad8,4": return .iPadPro11
case "iPad6,7", "iPad6,8": return .iPadPro12_9
case "iPad7,1", "iPad7,2": return .iPadPro12_9_2nd
case "iPad8,5", "iPad8,6", "iPad8,7", "iPad8,8": return .iPadPro12_9_3rd
case "iPad2,5", "iPad2,6", "iPad2,7": return .iPadMini1
case "iPad4,4", "iPad4,5", "iPad4,6": return .iPadMini2
case "iPad4,7", "iPad4,8", "iPad4,9": return .iPadMini3
case "iPad5,1", "iPad5,2": return .iPadMini4
case "iPhone1,1": return .iPhone
case "iPhone1,2": return .iPhone3g
case "iPhone2,1": return .iPhone3gs
case "iPhone3,1", "iPhone3,2", "iPhone3,3": return .iPhone4
case "iPhone4,1": return .iPhone4s
case "iPhone5,1", "iPhone5,2": return .iPhone5
case "iPhone5,3", "iPhone5,4": return .iPhone5c
case "iPhone6,1", "iPhone6,2": return .iPhone5s
case "iPhone7,1": return .iPhone6Plus
case "iPhone7,2": return .iPhone6
case "iPhone8,1": return .iPhone6s
case "iPhone8,2": return .iPhone6sPlus
case "iPhone8,4": return .iPhoneSE
case "iPhone9,1", "iPhone9,3": return .iPhone7
case "iPhone9,2", "iPhone9,4": return .iPhone7Plus
case "iPhone10,1", "iPhone10,4": return .iPhone8
case "iPhone10,2","iPhone10,5": return .iPhone8Plus
case "iPhone10,3", "iPhone10,6": return .iPhoneX
case "iPhone11,8": return .iPhoneXR
case "iPhone11,2": return .iPhoneXS
case "iPhone11,4","iPhone11,6": return .iPhoneXSMax
case "iPod1,1": return .iPod1
case "iPod2,1": return .iPod2
case "iPod3,1": return .iPod3
case "iPod4,1": return .iPod4
case "iPod5,1": return .iPod5
case "iPod7,1": return .iPod6
case "AirPods1,1": return .airPods
case "AppleTV2,1": return .appleTV2
case "AppleTV3,1", "AppleTV3,2": return .appleTV3
case "AppleTV5,3": return .appleTV4
case "AppleTV6,2": return .appleTV4K
case "Watch1,1", "Watch1,2": return .appleWatch
case "Watch2,6", "Watch2,7": return .appleWatchS1
case "Watch2,3", "Watch2,4": return .appleWatchS2
case "Watch3,1", "Watch3,2", "Watch3,3", "Watch3,4":return .appleWatchS3
case "Watch4,1", "Watch4,2", "Watch4,3", "Watch4,4":return .appleWatchS4
default:
return nil
}
}
var modelName: String {
#if targetEnvironment(simulator)
let identifier = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"]!
#else
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)))
}
#endif
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", "iPhone9,3": return "iPhone 7"
case "iPhone9,2", "iPhone9,4": return "iPhone 7 Plus"
case "iPhone8,4": return "iPhone SE"
case "iPhone10,1", "iPhone10,4": return "iPhone 8"
case "iPhone10,2", "iPhone10,5": return "iPhone 8 Plus"
case "iPhone10,3", "iPhone10,6": return "iPhone X"
case "iPhone11,2": return "iPhone XS"
case "iPhone11,4", "iPhone11,6": return "iPhone XS Max"
case "iPhone11,8": return "iPhone XR"
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 "iPad6,11", "iPad6,12": return "iPad 5"
case "iPad7,5", "iPad7,6": return "iPad 6"
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,3", "iPad6,4": return "iPad Pro 9.7 Inch"
case "iPad6,7", "iPad6,8": return "iPad Pro 12.9 Inch"
case "iPad7,1", "iPad7,2": return "iPad Pro (12.9-inch) (2nd generation)"
case "iPad7,3", "iPad7,4": return "iPad Pro (10.5-inch)"
case "iPad8,1", "iPad8,2", "iPad8,3", "iPad8,4":return "iPad Pro (11-inch)"
case "iPad8,5", "iPad8,6", "iPad8,7", "iPad8,8":return "iPad Pro (12.9-inch) (3rd generation)"
case "AppleTV5,3": return "Apple TV"
case "AppleTV6,2": return "Apple TV 4K"
case "AudioAccessory1,1": return "HomePod"
default: return identifier
}
}
}