-
Notifications
You must be signed in to change notification settings - Fork 9
/
DefaultUserProvider.swift
248 lines (224 loc) · 10.9 KB
/
DefaultUserProvider.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
//
// AmplitudeContextProvider.swift
// Experiment
//
// Copyright © 2020 Amplitude. All rights reserved.
//
import Foundation
@objc public class DefaultUserProvider : NSObject, ExperimentUserProvider {
private let userId: String?
private let deviceId: String?
private let version: String?
private let language: String?
private let platform: String
private let os: String
private let deviceManufacturer: String
private let deviceModel: String
@objc public init(userId: String? = nil, deviceId: String? = nil) {
self.userId = userId
self.deviceId = deviceId
self.version = DefaultUserProvider.getVersion()
self.language = DefaultUserProvider.getLanguage()
self.platform = DefaultUserProvider.getPlatform()
self.os = DefaultUserProvider.getOs()
self.deviceManufacturer = DefaultUserProvider.getDeviceManufacturer()
self.deviceModel = DefaultUserProvider.getDeviceModel()
}
@objc public func getUser() -> ExperimentUser {
return ExperimentUserBuilder()
.deviceId(deviceId)
.userId(userId)
.version(version)
.language(language)
.platform(platform)
.os(os)
.deviceManufacturer(deviceManufacturer)
.deviceModel(deviceModel)
.build()
}
private static func getVersion() -> String? {
return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
}
private static func getLanguage() -> String? {
return Locale(identifier: "en_US").localizedString(forLanguageCode: Locale.preferredLanguages[0])
}
private static func getPlatform() -> String {
return "iOS"
}
private static func getOs() -> String {
let systemVersion = ProcessInfo.processInfo.operatingSystemVersion
let os = "ios \(systemVersion.majorVersion).\(systemVersion.minorVersion).\(systemVersion.patchVersion)."
return os
}
private static func getDeviceManufacturer() -> String {
return "Apple"
}
private static func getPlatformString() -> String {
var sysinfo = utsname()
uname(&sysinfo) // ignore return value
return String(bytes: Data(bytes: &sysinfo.machine, count: Int(_SYS_NAMELEN)), encoding: .ascii)!.trimmingCharacters(in: .controlCharacters)
}
private static func getDeviceModel() -> String {
let platform = getPlatformString()
// == iPhone ==
// iPhone 1
if (platform == "iPhone1,1") { return "iPhone 1" }
// iPhone 3
if (platform == "iPhone1,2") { return "iPhone 3G" }
if (platform == "iPhone2,1") { return "iPhone 3GS" }
// iPhone 4
if (platform == "iPhone3,1") { return "iPhone 4" }
if (platform == "iPhone3,2") { return "iPhone 4" }
if (platform == "iPhone3,3") { return "iPhone 4" }
if (platform == "iPhone4,1") { return "iPhone 4S" }
// iPhone 5
if (platform == "iPhone5,1") { return "iPhone 5" }
if (platform == "iPhone5,2") { return "iPhone 5" }
if (platform == "iPhone5,3") { return "iPhone 5c" }
if (platform == "iPhone5,4") { return "iPhone 5c" }
if (platform == "iPhone6,1") { return "iPhone 5s" }
if (platform == "iPhone6,2") { return "iPhone 5s" }
// iPhone 6
if (platform == "iPhone7,1") { return "iPhone 6 Plus" }
if (platform == "iPhone7,2") { return "iPhone 6" }
if (platform == "iPhone8,1") { return "iPhone 6s" }
if (platform == "iPhone8,2") { return "iPhone 6s Plus" }
// iPhone 7
if (platform == "iPhone9,1") { return "iPhone 7" }
if (platform == "iPhone9,2") { return "iPhone 7 Plus" }
if (platform == "iPhone9,3") { return "iPhone 7" }
if (platform == "iPhone9,4") { return "iPhone 7 Plus" }
// iPhone 8
if (platform == "iPhone10,1") { return "iPhone 8" }
if (platform == "iPhone10,4") { return "iPhone 8" }
if (platform == "iPhone10,2") { return "iPhone 8 Plus" }
if (platform == "iPhone10,5") { return "iPhone 8 Plus" }
// iPhone X
if (platform == "iPhone10,3") { return "iPhone X" }
if (platform == "iPhone10,6") { return "iPhone X" }
// iPhone XS
if (platform == "iPhone11,2") { return "iPhone XS" }
if (platform == "iPhone11,6") { return "iPhone XS Max" }
// iPhone XR
if (platform == "iPhone11,8") { return "iPhone XR" }
// iPhone 11
if (platform == "iPhone12,1") { return "iPhone 11" }
if (platform == "iPhone12,3") { return "iPhone 11 Pro" }
if (platform == "iPhone12,5") { return "iPhone 11 Pro Max" }
// iPhone SE
if (platform == "iPhone8,4") { return "iPhone SE" }
if (platform == "iPhone12,8") { return "iPhone SE 2" }
// == iPod ==
if (platform == "iPod1,1") { return "iPod Touch 1G" }
if (platform == "iPod2,1") { return "iPod Touch 2G" }
if (platform == "iPod3,1") { return "iPod Touch 3G" }
if (platform == "iPod4,1") { return "iPod Touch 4G" }
if (platform == "iPod5,1") { return "iPod Touch 5G" }
if (platform == "iPod7,1") { return "iPod Touch 6G" }
if (platform == "iPod9,1") { return "iPod Touch 7G" }
// == iPad ==
// iPad 1
if (platform == "iPad1,1") { return "iPad 1" }
// iPad 2
if (platform == "iPad2,1") { return "iPad 2" }
if (platform == "iPad2,2") { return "iPad 2" }
if (platform == "iPad2,3") { return "iPad 2" }
if (platform == "iPad2,4") { return "iPad 2" }
// iPad 3
if (platform == "iPad3,1") { return "iPad 3" }
if (platform == "iPad3,2") { return "iPad 3" }
if (platform == "iPad3,3") { return "iPad 3" }
// iPad 4
if (platform == "iPad3,4") { return "iPad 4" }
if (platform == "iPad3,5") { return "iPad 4" }
if (platform == "iPad3,6") { return "iPad 4" }
// iPad Air
if (platform == "iPad4,1") { return "iPad Air" }
if (platform == "iPad4,2") { return "iPad Air" }
if (platform == "iPad4,3") { return "iPad Air" }
// iPad Air 2
if (platform == "iPad5,3") { return "iPad Air 2" }
if (platform == "iPad5,4") { return "iPad Air 2" }
// iPad 5
if (platform == "iPad6,11") { return "iPad 5" }
if (platform == "iPad6,12") { return "iPad 5" }
// iPad 6
if (platform == "iPad7,5") { return "iPad 6" }
if (platform == "iPad7,6") { return "iPad 6" }
// iPad Air 3
if (platform == "iPad11,3") { return "iPad Air 3" }
if (platform == "iPad11,4") { return "iPad Air 3" }
// iPad 7
if (platform == "iPad7,11") { return "iPad 6" }
if (platform == "iPad7,12") { return "iPad 6" }
// iPad Pro
if (platform == "iPad6,7") { return "iPad Pro" }
if (platform == "iPad6,8") { return "iPad Pro" }
if (platform == "iPad6,3") { return "iPad Pro" }
if (platform == "iPad6,4") { return "iPad Pro" }
if (platform == "iPad7,1") { return "iPad Pro" }
if (platform == "iPad7,2") { return "iPad Pro" }
if (platform == "iPad7,3") { return "iPad Pro" }
if (platform == "iPad7,4") { return "iPad Pro" }
if (platform == "iPad8,1") { return "iPad Pro" }
if (platform == "iPad8,2") { return "iPad Pro" }
if (platform == "iPad8,3") { return "iPad Pro" }
if (platform == "iPad8,4") { return "iPad Pro" }
if (platform == "iPad8,5") { return "iPad Pro" }
if (platform == "iPad8,6") { return "iPad Pro" }
if (platform == "iPad8,7") { return "iPad Pro" }
if (platform == "iPad8,8") { return "iPad Pro" }
// iPad Mini
if (platform == "iPad2,5") { return "iPad Mini" }
if (platform == "iPad2,6") { return "iPad Mini" }
if (platform == "iPad2,7") { return "iPad Mini" }
// iPad Mini 2
if (platform == "iPad4,4") { return "iPad Mini 2" }
if (platform == "iPad4,5") { return "iPad Mini 2" }
if (platform == "iPad4,6") { return "iPad Mini 2" }
// iPad Mini 3
if (platform == "iPad4,7") { return "iPad Mini 3" }
if (platform == "iPad4,8") { return "iPad Mini 3" }
if (platform == "iPad4,9") { return "iPad Mini 3" }
// iPad Mini 4
if (platform == "iPad5,1") { return "iPad Mini 4" }
if (platform == "iPad5,2") { return "iPad Mini 4" }
// iPad Mini 5
if (platform == "iPad11,1") { return "iPad Mini 5" }
if (platform == "iPad11,2") { return "iPad Mini 5" }
// == Apple Watch ==
if (platform == "Watch1,1") { return "Apple Watch 38mm" }
if (platform == "Watch1,2") { return "Apple Watch 42mm" }
if (platform == "Watch2,3") { return "Apple Watch Series 2 38mm" }
if (platform == "Watch2,4") { return "Apple Watch Series 2 42mm" }
if (platform == "Watch2,6") { return "Apple Watch Series 1 38mm" }
if (platform == "Watch2,7") { return "Apple Watch Series 1 42mm" }
if (platform == "Watch3,1") { return "Apple Watch Series 3 38mm Cellular" }
if (platform == "Watch3,2") { return "Apple Watch Series 3 42mm Cellular" }
if (platform == "Watch3,3") { return "Apple Watch Series 3 38mm" }
if (platform == "Watch3,4") { return "Apple Watch Series 3 42mm" }
if (platform == "Watch4,1") { return "Apple Watch Series 4 40mm" }
if (platform == "Watch4,2") { return "Apple Watch Series 4 44mm" }
if (platform == "Watch4,3") { return "Apple Watch Series 4 40mm Cellular" }
if (platform == "Watch4,4") { return "Apple Watch Series 4 44mm Cellular" }
if (platform == "Watch5,1") { return "Apple Watch Series 5 40mm" }
if (platform == "Watch5,2") { return "Apple Watch Series 5 44mm" }
if (platform == "Watch5,3") { return "Apple Watch Series 5 40mm Cellular" }
if (platform == "Watch5,4") { return "Apple Watch Series 5 44mm Cellular" }
if (platform == "Watch6,1") { return "Apple Watch Series 6 40mm" }
if (platform == "Watch6,2") { return "Apple Watch Series 6 44mm" }
if (platform == "Watch6,3") { return "Apple Watch Series 6 40mm Cellular" }
if (platform == "Watch6,4") { return "Apple Watch Series 6 44mm Cellular" }
// == Others ==
if (platform == "i386") { return "Simulator" }
if (platform == "x86_64") { return "Simulator" }
if (platform.hasPrefix("MacBookAir")) { return "MacBook Air" }
if (platform.hasPrefix("MacBookPro")) { return "MacBook Pro" }
if (platform.hasPrefix("MacBook")) { return "MacBook" }
if (platform.hasPrefix("MacPro")) { return "Mac Pro" }
if (platform.hasPrefix("Macmini")) { return "Mac Mini" }
if (platform.hasPrefix("iMac")) { return "iMac" }
if (platform.hasPrefix("Xserve")) { return "Xserve" }
return platform
}
}