-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIDevice+Extensions.swift
311 lines (282 loc) · 13.7 KB
/
UIDevice+Extensions.swift
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
306
307
308
309
310
311
//
// UIDevice+Extensions.swift
// OYExtensions
//
// Created by osmanyildirim
//
import UIKit
#if canImport(AdSupport)
import AdSupport
#endif
#if canImport(AppTrackingTransparency)
import AppTrackingTransparency
#endif
#if canImport(AudioToolbox)
import AudioToolbox
#endif
#if canImport(AVFoundation)
import AVFoundation
#endif
extension UIDevice {
/// `UIDevice.oy_current` → output → <UIDevice: 0x6000013e0030>
public static var oy_current: UIDevice {
UIDevice.current
}
/// `UIDevice.oy_vendorId` → output → "98BBB96B-9A54-4285-8DBF-6BBC4AF0674C"
public static var oy_vendorId: String? {
oy_current.identifierForVendor?.uuidString
}
/// `UIDevice.oy_advertisingId` → output → "123e4567-e89b-12d3-a456–426614174000"
/// Your app must request tracking authorization before it can get the advertising identifier
public static func oy_advertisingId() throws -> String? {
let identifierManager = ASIdentifierManager.shared()
if #available(iOS 14, *) {
if ATTrackingManager.trackingAuthorizationStatus == .authorized {
return identifierManager.advertisingIdentifier.uuidString
}
} else if identifierManager.isAdvertisingTrackingEnabled {
return identifierManager.advertisingIdentifier.uuidString
}
throw OYError.advertisingTrackingIsNotEnabled
}
/// Create and return a brand new unique identifier
/// `UIDevice.oy_uuid` → output → "68FCCB58-23A5-47BC-AA56-E0757F1BDBEA"
public static var oy_uuid: String {
NSUUID().uuidString
}
/// `UIDevice.oy_globallyUnique` → output → "7009F12B-64FF-4F69-8C5C-E8A1F3F4BD8A-20863-0000055A7CF567F7"
public static var oy_globallyUnique: String {
ProcessInfo.processInfo.globallyUniqueString
}
/// `UIDevice.oy_deviceName` → output → "iPhone 14 Pro Max"
public static var oy_deviceName: String {
oy_current.name
}
/// `UIDevice.oy_modelName` → output → "Simulator iPhone 14 Pro Max"
public static var oy_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)))
}
return oy_deviceModel(identifier)
}
/// `UIDevice.oy_systemName` → output → "iOS"
public static var oy_systemName: String {
oy_current.systemName
}
/// `UIDevice.oy_systemVersion` → output → "16.0"
public static var oy_systemVersion: String {
oy_current.systemVersion
}
/// `UIDevice.oy_systemVersionFloat` → output → 16.0
public static var oy_systemVersionFloat: Float? {
oy_systemVersion.oy_float
}
/// `UIDevice.oy_language` → output → "en"
public static var oy_language: String? {
Bundle.main.oy_langugage
}
/// `UIDevice.oy_isPhone` → output → true
public static var oy_isPhone: Bool {
oy_current.userInterfaceIdiom == UIUserInterfaceIdiom.phone
}
/// `UIDevice.oy_isPad` → output → false
public static var oy_isPad: Bool {
oy_current.userInterfaceIdiom == UIUserInterfaceIdiom.pad
}
/// `UIDevice.oy_hasNotch` → output → true
public static var oy_hasNotch: Bool {
let bottom = UIApplication.oy_keyWindow?.safeAreaInsets.bottom ?? 0
return bottom > 0
}
/// `UIDevice.oy_isJailbroken` → output → false
public static var oy_isJailbroken: Bool {
do {
let path = "/private/jailbreak.txt"
try "OyExtensions Jailbroken Detection Test Message".write(toFile: path, atomically: true, encoding: .utf8)
try FileManager.default.removeItem(atPath: path)
return true
} catch {
return false
}
}
/// `UIDevice.oy_isMac` → output → false
public static var oy_isMac: Bool {
if #available(iOS 14.0, tvOS 14.0, *) {
if UIDevice.current.userInterfaceIdiom == .mac {
return true
}
}
#if targetEnvironment(macCatalyst)
return true
#else
return false
#endif
}
/// On/Off device's torch (camera flash)
/// `UIDevice.oy_toggleTorch()`
public static func oy_toggleTorch(level: Float = 1.0) {
guard let device = AVCaptureDevice.default(for: .video), device.hasTorch else { return }
try? device.lockForConfiguration()
if device.isTorchActive {
device.torchMode = .off
} else {
try? device.setTorchModeOn(level: level)
}
device.unlockForConfiguration()
}
/// Make a vibration of device
/// `UIDevice.oy_vibrateDevice()`
public static func oy_vibrateDevice() {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
}
/// `UIDevice.oy_userInterfaceStyle` → output → .dark
@available(iOS 13.0, *)
public static var oy_userInterfaceStyle: UIUserInterfaceStyle {
UITraitCollection.current.userInterfaceStyle
}
/// `UIDevice.oy_isDarkMode` → output → true
@available(iOS 13.0, *)
public static var oy_isDarkMode: Bool {
UITraitCollection.current.userInterfaceStyle == .dark
}
/// `UIDevice.oy_isPortrait` → output → true
public static var oy_isPortrait: Bool {
UIDevice.current.orientation.isPortrait
}
/// `UIDevice.oy_isLandscape` → output → false
public static var oy_isLandscape: Bool {
UIDevice.current.orientation.isLandscape
}
}
extension UIDevice {
/// Get device model by identifier
private static func oy_deviceModel(_ identifier: String) -> String {
#if os(iOS)
switch identifier {
case "iPod5,1": return "iPod touch (5th generation)"
case "iPod7,1": return "iPod touch (6th generation)"
case "iPod9,1": return "iPod touch (7th generation)"
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 "iPhone8,4": return "iPhone SE"
case "iPhone9,1", "iPhone9,3": return "iPhone 7"
case "iPhone9,2", "iPhone9,4": return "iPhone 7 Plus"
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 "iPhone12,1": return "iPhone 11"
case "iPhone12,3": return "iPhone 11 Pro"
case "iPhone12,5": return "iPhone 11 Pro Max"
case "iPhone13,1": return "iPhone 12 mini"
case "iPhone13,2": return "iPhone 12"
case "iPhone13,3": return "iPhone 12 Pro"
case "iPhone13,4": return "iPhone 12 Pro Max"
case "iPhone12,8": return "iPhone SE (2nd generation)"
case "iPhone14,2": return "iPhone 13 Pro"
case "iPhone14,3": return "iPhone 13 Pro Max"
case "iPhone14,4": return "iPhone 13 mini"
case "iPhone14,5": return "iPhone 13"
case "iPhone14,6": return "iPhone SE (3rd generation)"
case "iPhone14,7": return "iPhone 14"
case "iPhone14,8": return "iPhone 14 Plus"
case "iPhone15,2": return "iPhone 14 Pro"
case "iPhone15,3": return "iPhone 14 Pro Max"
case "iPhone15,4": return "iPhone 15"
case "iPhone15,5": return "iPhone 15 Plus"
case "iPhone16,1": return "iPhone 15 Pro"
case "iPhone16,2": return "iPhone 15 Pro Max"
case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4": return "iPad 2"
case "iPad3,1", "iPad3,2", "iPad3,3": return "iPad (3rd generation)"
case "iPad3,4", "iPad3,5", "iPad3,6": return "iPad (4th generation)"
case "iPad6,11", "iPad6,12": return "iPad (5th generation)"
case "iPad7,5", "iPad7,6": return "iPad (6th generation)"
case "iPad7,11", "iPad7,12": return "iPad (7th generation)"
case "iPad11,6", "iPad11,7": return "iPad (8th generation)"
case "iPad12,1", "iPad12,2": return "iPad (9th generation)"
case "iPad4,1", "iPad4,2", "iPad4,3": return "iPad Air"
case "iPad5,3", "iPad5,4": return "iPad Air 2"
case "iPad11,3", "iPad11,4": return "iPad Air (3rd generation)"
case "iPad13,1", "iPad13,2": return "iPad Air (4th generation)"
case "iPad13,16", "iPad13,17": return "iPad Air (5th generation)"
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 "iPad11,1", "iPad11,2": return "iPad mini (5th generation)"
case "iPad14,1", "iPad14,2": return "iPad mini (6th generation)"
case "iPad6,3", "iPad6,4": return "iPad Pro (9.7-inch)"
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) (1st generation)"
case "iPad8,9", "iPad8,10": return "iPad Pro (11-inch) (2nd generation)"
case "iPad13,4", "iPad13,5", "iPad13,6", "iPad13,7": return "iPad Pro (11-inch) (3rd generation)"
case "iPad6,7", "iPad6,8": return "iPad Pro (12.9-inch) (1st generation)"
case "iPad7,1", "iPad7,2": return "iPad Pro (12.9-inch) (2nd generation)"
case "iPad8,5", "iPad8,6", "iPad8,7", "iPad8,8": return "iPad Pro (12.9-inch) (3rd generation)"
case "iPad8,11", "iPad8,12": return "iPad Pro (12.9-inch) (4th generation)"
case "iPad13,8", "iPad13,9", "iPad13,10", "iPad13,11": return "iPad Pro (12.9-inch) (5th generation)"
case "iPad14,3", "iPad14,4": return "iPad Pro 11-inch (4th generation)"
case "iPad14,5", "iPad14,6": return "iPad Pro 12.9-inch (6th generation)"
case "AudioAccessory1,1": return "HomePod"
case "AudioAccessory5,1": return "HomePod mini"
case "i386", "x86_64", "arm64": return "Simulator \(oy_deviceModel(ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "iOS"))"
default: return identifier
}
#elseif os(watchOS)
switch identifier {
case "Watch1,1": return "Apple Watch (1st generation) - 38mm"
case "Watch1,2": return "Apple Watch (1st generation) - 42mm"
case "Watch2,6": return "Apple Watch Series 1 - 38mm"
case "Watch2,7": return "Apple Watch Series 1 - 42mm"
case "Watch2,3": return "Apple Watch Series 2 - 38mm"
case "Watch2,4": return "Apple Watch Series 2 - 42mm"
case "Watch3,1", "Watch3,3": return "Apple Watch Series 3 - 38mm"
case "Watch3,2", "Watch3,4": return "Apple Watch Series 1 - 42mm"
case "Watch4,1", "Watch4,3": return "Apple Watch Series 4 - 40mm"
case "Watch4,2", "Watch4,4": return "Apple Watch Series 4 - 44mm"
case "Watch5,1", "Watch5,3": return "Apple Watch Series 5 - 40mm"
case "Watch5,2", "Watch5,4": return "Apple Watch Series 5 - 44mm"
case "Watch6,1", "Watch6,3": return "Apple Watch Series 6 - 40mm"
case "Watch6,2", "Watch6,4": return "Apple Watch Series 6 - 44mm"
case "Watch5,9", "Watch5,11": return "Apple Watch SE - 40mm"
case "Watch5,10", "Watch5,12": return "Apple Watch SE - 44mm"
case "Watch6,6", "Watch6,8": return "Apple Watch Series 7 - 41mm"
case "Watch6,7", "Watch6,9": return "Apple Watch Series 7 - 45mm"
case "Watch6,14", "Watch6,16": return "Apple Watch Series 8 - 41mm"
case "Watch6,15", "Watch6,17": return "Apple Watch Series 8 - 45mm"
case "Watch6,10", "Watch6,12": return "Apple Watch SE (2nd generation) - 40mm"
case "Watch6,11", "Watch6,13": return "Apple Watch SE (2nd generation) - 44mm"
case "Watch6,18": return "Apple Watch Ultra"
case "Watch7,3": return "Apple Watch Series 9 - 41mm"
case "Watch7,4": return "Apple Watch Series 9 - 45mm"
case "Watch7,5": return "Apple Watch Ultra2"
case "i386", "x86_64": return "Simulator \(oy_deviceModel(ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "watchOS"))"
default: return identifier
}
#elseif os(tvOS)
switch identifier {
case "AppleTV5,3": return "Apple TV"
case "AppleTV6,2": return "Apple TV 4K"
case "AppleTV11,1": return "Apple TV 4K (2nd generation)"
case "AppleTV14,1": return "Apple TV 4K (3rd generation"
case "i386", "x86_64": return "Simulator \(oy_deviceModel(ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "tvOS"))"
default: return identifier
}
#elseif os(visionOS)
return identifier
#endif
}
}