-
Notifications
You must be signed in to change notification settings - Fork 43
/
UIDeviceAdditions.m
230 lines (214 loc) · 8.9 KB
/
UIDeviceAdditions.m
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
// Copyright 2018-2019 Yubico AB
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import <sys/utsname.h>
#import "UIDeviceAdditions.h"
@implementation UIDevice(YKFDeviceAdditions)
static YKFDeviceModel ykf_deviceModelInternal = YKFDeviceModelUnknown;
- (YKFDeviceModel)ykf_setupDeviceModel {
#if TARGET_IPHONE_SIMULATOR
return YKFDeviceModelSimulator;
#else
struct utsname systemInfo;
uname(&systemInfo);
NSString *deviceName = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
// iPhone models
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone3,1", @"iPhone3,2", @"iPhone3,3"]]) {
return YKFDeviceModelIPhone4;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone4,1", @"iPhone4,2", @"iPhone4,3"]]) {
return YKFDeviceModelIPhone4S;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone5,1", @"iPhone5,2"]]) {
return YKFDeviceModelIPhone5;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone5,3", @"iPhone5,4"]]) {
return YKFDeviceModelIPhone5C;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone6,1", @"iPhone6,2"]]) {
return YKFDeviceModelIPhone5S;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone7,2"]]) {
return YKFDeviceModelIPhone6;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone7,1"]]) {
return YKFDeviceModelIPhone6Plus;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone8,1"]]) {
return YKFDeviceModelIPhone6S;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone8,2"]]) {
return YKFDeviceModelIPhone6SPlus;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone8,4"]]) {
return YKFDeviceModelIPhoneSE;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone9,1", @"iPhone9,3"]]) {
return YKFDeviceModelIPhone7;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone9,2", @"iPhone9,4"]]) {
return YKFDeviceModelIPhone7Plus;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone10,1", @"iPhone10,4"]]) {
return YKFDeviceModelIPhone8;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone10,2", @"iPhone10,5"]]) {
return YKFDeviceModelIPhone8Plus;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone10,3", @"iPhone10,6"]]) {
return YKFDeviceModelIPhoneX;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone11,2"]]) {
return YKFDeviceModelIPhoneXS;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone11,4", @"iPhone11,6"]]) {
return YKFDeviceModelIPhoneXSMax;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone11,8"]]) {
return YKFDeviceModelIPhoneXR;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone12,1", @"iPhone12,3", @"iPhone12,5"]]) {
return YKFDeviceModelIPhone11;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone12,8"]]) {
return YKFDeviceModelIPhoneSE2;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone13,1", @"iPhone13,2", @"iPhone13,3", @"iPhone13,4"]]) {
return YKFDeviceModelIPhone12;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone14,2", @"iPhone14,3", @"iPhone14,4", @"iPhone14,5"]]) {
return YKFDeviceModelIPhone13;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone14,6"]]) {
return YKFDeviceModelIPhoneSE3;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone14,7", @"iPhone14,8", @"iPhone15,2", @"iPhone15,3"]]) {
return YKFDeviceModelIPhone14;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPhone15,4", @"iPhone15,5", @"iPhone16,1", @"iPhone16,2"]]) {
return YKFDeviceModelIPhone15;
}
// iPad models
if ([self ykf_deviceName:deviceName isInList:@[@"iPad1,1"]]) {
return YKFDeviceModelIPad1;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad2,1", @"iPad2,2", @"iPad2,3", @"iPad2,4"]]) {
return YKFDeviceModelIPad2;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad3,1", @"iPad3,2", @"iPad3,3"]]) {
return YKFDeviceModelIPad3;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad3,4", @"iPad3,5", @"iPad3,6"]]) {
return YKFDeviceModelIPad4;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad4,1", @"iPad4,2", @"iPad4,3"]]) {
return YKFDeviceModelIPadAir;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad5,3", @"iPad5,4"]]) {
return YKFDeviceModelIPadAir2;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad2,5", @"iPad2,6", @"iPad2,7"]]) {
return YKFDeviceModelIPadMini;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad4,4", @"iPad4,5", @"iPad4,6"]]) {
return YKFDeviceModelIPadMini2;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad4,7", @"iPad4,8", @"iPad4,9"]]) {
return YKFDeviceModelIPadMini3;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad5,1", @"iPad5,2"]]) {
return YKFDeviceModelIPadMini4;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad6,3", @"iPad6,4", @"iPad6,7", @"iPad6,8"]]) {
return YKFDeviceModelIPadPro;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad6,11", @"iPad6,12"]]) {
return YKFDeviceModelIPad2017;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad7,1", @"iPad7,2", @"iPad7,3", @"iPad7,4"]]) {
return YKFDeviceModelIPadPro2;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad7,5", @"iPad7,6"]]) {
return YKFDeviceModelIPad6;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad8,1", @"iPad8,2", @"iPad8,3", @"iPad8,4", @"iPad8,5", @"iPad8,6", @"iPad8,7", @"iPad8,8"]]) {
return YKFDeviceModelIPadPro3;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad11,1", @"iPad11,2"]]) {
return YKFDeviceModelIPadMini5;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad11,3", @"iPad11,4"]]) {
return YKFDeviceModelIPadAir3;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad12,1", @"iiPad12,2"]]) {
return YKFDeviceModelIPad9;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad14,1", @"iPad14,2"]]) {
return YKFDeviceModelIPadMini6;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad8,9", @"iPad8,10", @"iPad8,11", @"iPad8,12"]]) {
return YKFDeviceModelIPadPro4;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad13,1", @"iPad13,2"]]) {
return YKFDeviceModelIPadAir4;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad13,4", @"iPad13,5", @"iPad13,6", @"iPad13,7", @"iPad13,8", @"iPad13,9", @"iPad13,10", @"iPad13,11"]]) {
return YKFDeviceModelIPadPro5;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad13,16", @"iPad13,17"]]) {
return YKFDeviceModelIPadAir5;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad13,18", @"iPad13,19"]]) {
return YKFDeviceModelIPad10;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPad14,3", @"iPad14,4", @"iPad14,5", @"iPad14,6"]]) {
return YKFDeviceModelIPadPro6;
}
// iPod models
if ([self ykf_deviceName:deviceName isInList:@[@"iPod1,1"]]) {
return YKFDeviceModelIPodTouch1;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPod2,1"]]) {
return YKFDeviceModelIPodTouch2;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPod3,1"]]) {
return YKFDeviceModelIPodTouch3;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPod4,1"]]) {
return YKFDeviceModelIPodTouch4;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPod5,1"]]) {
return YKFDeviceModelIPodTouch5;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPod7,1"]]) {
return YKFDeviceModelIPodTouch6;
}
if ([self ykf_deviceName:deviceName isInList:@[@"iPod9,1"]]) {
return YKFDeviceModelIPodTouch7;
}
// Unknown device
return YKFDeviceModelUnknown;
#endif
}
- (BOOL)ykf_deviceName:(NSString *)deviceName isInList:(NSArray *)deviceList {
return [deviceList containsObject:deviceName];
}
- (YKFDeviceModel)ykf_deviceModel {
// The device type does not change at runtime so set it up once.
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
ykf_deviceModelInternal = [self ykf_setupDeviceModel];
});
return ykf_deviceModelInternal;
}
@end