/
UIDevice+ADJAdditions.m
359 lines (308 loc) · 12.2 KB
/
UIDevice+ADJAdditions.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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//
// UIDevice+ADJAdditions.m
// Adjust
//
// Created by Christian Wellenbrock (@wellle) on 23rd July 2012.
// Copyright © 2012-2018 Adjust GmbH. All rights reserved.
//
#import "UIDevice+ADJAdditions.h"
#import "NSString+ADJAdditions.h"
#import <sys/sysctl.h>
#if !ADJUST_NO_IDFA
#import <AdSupport/ASIdentifierManager.h>
#endif
#if !ADJUST_NO_IAD && !TARGET_OS_TV
#import <iAd/iAd.h>
#endif
#import "ADJUtil.h"
#import "ADJSystemProfile.h"
#import "ADJAdjustFactory.h"
@implementation UIDevice(ADJAdditions)
- (Class)adSupportManager {
NSString *className = [NSString adjJoin:@"A", @"S", @"identifier", @"manager", nil];
Class class = NSClassFromString(className);
return class;
}
- (Class)appTrackingManager {
NSString *className = [NSString adjJoin:@"A", @"T", @"tracking", @"manager", nil];
Class class = NSClassFromString(className);
return class;
}
- (int)adjATTStatus {
Class appTrackingClass = [self appTrackingManager];
if (appTrackingClass != nil) {
NSString *keyAuthorization = [NSString adjJoin:@"tracking", @"authorization", @"status", nil];
SEL selAuthorization = NSSelectorFromString(keyAuthorization);
if ([appTrackingClass respondsToSelector:selAuthorization]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability"
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
return (int)[appTrackingClass performSelector:selAuthorization];
#pragma clang diagnostic pop
}
}
return -1;
}
- (void)requestTrackingAuthorizationWithCompletionHandler:(void (^)(NSUInteger status))completion
{
Class appTrackingClass = [self appTrackingManager];
if (appTrackingClass == nil) {
return;
}
NSString *requestAuthorization = [NSString adjJoin:
@"request",
@"tracking",
@"authorization",
@"with",
@"completion",
@"handler:", nil];
SEL selRequestAuthorization = NSSelectorFromString(requestAuthorization);
if (![appTrackingClass respondsToSelector:selRequestAuthorization]) {
return;
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability"
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[appTrackingClass performSelector:selRequestAuthorization withObject:completion];
#pragma clang diagnostic pop
}
- (BOOL)adjTrackingEnabled {
#if ADJUST_NO_IDFA
return NO;
#else
// return [[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled];
Class adSupportClass = [self adSupportManager];
if (adSupportClass == nil) {
return NO;
}
NSString *keyManager = [NSString adjJoin:@"shared", @"manager", nil];
SEL selManager = NSSelectorFromString(keyManager);
if (![adSupportClass respondsToSelector:selManager]) {
return NO;
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
id manager = [adSupportClass performSelector:selManager];
NSString *keyEnabled = [NSString adjJoin:@"is", @"advertising", @"tracking", @"enabled", nil];
SEL selEnabled = NSSelectorFromString(keyEnabled);
if (![manager respondsToSelector:selEnabled]) {
return NO;
}
BOOL enabled = (BOOL)[manager performSelector:selEnabled];
return enabled;
#pragma clang diagnostic pop
#endif
}
- (NSString *)adjIdForAdvertisers {
#if ADJUST_NO_IDFA
return @"";
#else
// return [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
Class adSupportClass = [self adSupportManager];
if (adSupportClass == nil) {
return @"";
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
NSString *keyManager = [NSString adjJoin:@"shared", @"manager", nil];
SEL selManager = NSSelectorFromString(keyManager);
if (![adSupportClass respondsToSelector:selManager]) {
return @"";
}
id manager = [adSupportClass performSelector:selManager];
NSString *keyIdentifier = [NSString adjJoin:@"advertising", @"identifier", nil];
SEL selIdentifier = NSSelectorFromString(keyIdentifier);
if (![manager respondsToSelector:selIdentifier]) {
return @"";
}
id identifier = [manager performSelector:selIdentifier];
NSString *keyString = [NSString adjJoin:@"UUID", @"string", nil];
SEL selString = NSSelectorFromString(keyString);
if (![identifier respondsToSelector:selString]) {
return @"";
}
NSString *string = [identifier performSelector:selString];
return string;
#pragma clang diagnostic pop
#endif
}
- (NSString *)adjFbAnonymousId {
#if TARGET_OS_TV
return @"";
#else
// pre FB SDK v6.0.0
// return [FBSDKAppEventsUtility retrievePersistedAnonymousID];
// post FB SDK v6.0.0
// return [FBSDKBasicUtility retrievePersistedAnonymousID];
Class class = nil;
SEL selGetId = NSSelectorFromString(@"retrievePersistedAnonymousID");
class = NSClassFromString(@"FBSDKBasicUtility");
if (class != nil) {
if ([class respondsToSelector:selGetId]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
NSString *fbAnonymousId = (NSString *)[class performSelector:selGetId];
return fbAnonymousId;
#pragma clang diagnostic pop
}
}
class = NSClassFromString(@"FBSDKAppEventsUtility");
if (class != nil) {
if ([class respondsToSelector:selGetId]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
NSString *fbAnonymousId = (NSString *)[class performSelector:selGetId];
return fbAnonymousId;
#pragma clang diagnostic pop
}
}
return @"";
#endif
}
- (NSString *)adjDeviceType {
NSString *type = [self.model stringByReplacingOccurrencesOfString:@" " withString:@""];
return type;
}
- (NSString *)adjDeviceName {
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *name = calloc(1, size);
sysctlbyname("hw.machine", name, &size, NULL, 0);
NSString *machine = [NSString stringWithUTF8String:name];
free(name);
return machine;
}
- (NSString *)adjCreateUuid {
CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault);
CFStringRef stringRef = CFUUIDCreateString(kCFAllocatorDefault, newUniqueId);
NSString *uuidString = (__bridge_transfer NSString*)stringRef;
NSString *lowerUuid = [uuidString lowercaseString];
CFRelease(newUniqueId);
return lowerUuid;
}
- (NSString *)adjVendorId {
if ([UIDevice.currentDevice respondsToSelector:@selector(identifierForVendor)]) {
return [UIDevice.currentDevice.identifierForVendor UUIDString];
}
return @"";
}
- (void)adjCheckForiAd:(ADJActivityHandler *)activityHandler queue:(dispatch_queue_t)queue {
// if no tries for iad v3 left, stop trying
id<ADJLogger> logger = [ADJAdjustFactory logger];
#if ADJUST_NO_IAD || TARGET_OS_TV
[logger debug:@"ADJUST_NO_IAD or TARGET_OS_TV set"];
return;
#else
[logger debug:@"ADJUST_NO_IAD or TARGET_OS_TV not set"];
// [[ADClient sharedClient] ...]
Class ADClientClass = NSClassFromString(@"ADClient");
if (ADClientClass == nil) {
[logger warn:@"iAd framework not found in user's app (ADClientClass not found)"];
return;
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
SEL sharedClientSelector = NSSelectorFromString(@"sharedClient");
if (![ADClientClass respondsToSelector:sharedClientSelector]) {
[logger warn:@"iAd framework not found in user's app (sharedClient method not found)"];
return;
}
id ADClientSharedClientInstance = [ADClientClass performSelector:sharedClientSelector];
if (ADClientSharedClientInstance == nil) {
[logger warn:@"iAd framework not found in user's app (ADClientSharedClientInstance is nil)"];
return;
}
[logger debug:@"iAd framework successfully found in user's app"];
BOOL iAdInformationAvailable = [self setiAdWithDetails:activityHandler
adcClientSharedInstance:ADClientSharedClientInstance
queue:queue];
if (!iAdInformationAvailable) {
[logger warn:@"iAd information not available"];
return;
}
#pragma clang diagnostic pop
#endif
}
- (NSString *)adjFetchAdServicesAttribution:(NSError **)errorPtr {
id<ADJLogger> logger = [ADJAdjustFactory logger];
// [AAAttribution attributionTokenWithError:...]
Class attributionClass = NSClassFromString(@"AAAttribution");
if (attributionClass == nil) {
[logger warn:@"AdServices framework not found in user's app (AAAttribution not found)"];
if (errorPtr) {
*errorPtr = [NSError errorWithDomain:@"com.adjust.sdk.adServices"
code:100
userInfo:@{@"Error reason": @"AdServices framework not found"}];
}
return nil;
}
SEL attributionTokenSelector = NSSelectorFromString(@"attributionTokenWithError:");
if (![attributionClass respondsToSelector:attributionTokenSelector]) {
if (errorPtr) {
*errorPtr = [NSError errorWithDomain:@"com.adjust.sdk.adServices"
code:100
userInfo:@{@"Error reason": @"AdServices framework not found"}];
}
return nil;
}
NSMethodSignature *attributionTokenMethodSignature = [attributionClass methodSignatureForSelector:attributionTokenSelector];
NSInvocation *tokenInvocation = [NSInvocation invocationWithMethodSignature:attributionTokenMethodSignature];
[tokenInvocation setSelector:attributionTokenSelector];
[tokenInvocation setTarget:attributionClass];
__autoreleasing NSError *error;
__autoreleasing NSError **errorPointer = &error;
[tokenInvocation setArgument:&errorPointer atIndex:2];
[tokenInvocation invoke];
if (error) {
[logger error:@"Error while retrieving AdServices attribution token: %@", error];
if (errorPtr) {
*errorPtr = error;
}
return nil;
}
NSString * __unsafe_unretained tmpToken = nil;
[tokenInvocation getReturnValue:&tmpToken];
NSString *token = tmpToken;
return token;
}
- (BOOL)setiAdWithDetails:(ADJActivityHandler *)activityHandler
adcClientSharedInstance:(id)ADClientSharedClientInstance
queue:(dispatch_queue_t)queue {
SEL iAdDetailsSelector = NSSelectorFromString(@"requestAttributionDetailsWithBlock:");
if (![ADClientSharedClientInstance respondsToSelector:iAdDetailsSelector]) {
return NO;
}
__block Class lock = [ADJActivityHandler class];
__block BOOL completed = NO;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[ADClientSharedClientInstance performSelector:iAdDetailsSelector
withObject:^(NSDictionary *attributionDetails, NSError *error) {
@synchronized (lock) {
if (completed) {
return;
} else {
completed = YES;
}
}
[activityHandler setAttributionDetails:attributionDetails
error:error];
}];
#pragma clang diagnostic pop
// 5 seconds of timeout
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), queue, ^{
@synchronized (lock) {
if (completed) {
return;
} else {
completed = YES;
}
}
[activityHandler setAttributionDetails:nil
error:[NSError errorWithDomain:@"com.adjust.sdk.iAd"
code:100
userInfo:@{@"Error reason": @"iAd request timed out"}]];
});
return YES;
}
@end