-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
FBSDKAppEventsUtility.m
474 lines (401 loc) · 16.4 KB
/
FBSDKAppEventsUtility.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKAppEventsUtility.h"
#import <objc/runtime.h>
#import <AdSupport/AdSupport.h>
#import "FBSDKAccessToken.h"
#import "FBSDKAppEvents.h"
#import "FBSDKAppEventsDeviceInfo.h"
#import "FBSDKConstants.h"
#import "FBSDKDynamicFrameworkLoader.h"
#import "FBSDKError.h"
#import "FBSDKInternalUtility.h"
#import "FBSDKLogger.h"
#import "FBSDKSettings.h"
#import "FBSDKTimeSpentData.h"
#define FBSDK_APPEVENTSUTILITY_ANONYMOUSIDFILENAME @"com-facebook-sdk-PersistedAnonymousID.json"
#define FBSDK_APPEVENTSUTILITY_ANONYMOUSID_KEY @"anon_id"
#define FBSDK_APPEVENTSUTILITY_MAX_IDENTIFIER_LENGTH 40
@implementation FBSDKAppEventsUtility
+ (NSMutableDictionary *)activityParametersDictionaryForEvent:(NSString *)eventCategory
implicitEventsOnly:(BOOL)implicitEventsOnly
shouldAccessAdvertisingID:(BOOL)shouldAccessAdvertisingID {
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
parameters[@"event"] = eventCategory;
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
NSString *attributionID = [[self class] attributionID]; // Only present on iOS 6 and below.
[FBSDKInternalUtility dictionary:parameters setObject:attributionID forKey:@"attribution"];
#endif
if (!implicitEventsOnly && shouldAccessAdvertisingID) {
NSString *advertiserID = [[self class] advertiserID];
[FBSDKInternalUtility dictionary:parameters setObject:advertiserID forKey:@"advertiser_id"];
}
parameters[FBSDK_APPEVENTSUTILITY_ANONYMOUSID_KEY] = [self anonymousID];
FBSDKAdvertisingTrackingStatus advertisingTrackingStatus = [[self class] advertisingTrackingStatus];
if (advertisingTrackingStatus != FBSDKAdvertisingTrackingUnspecified) {
BOOL allowed = (advertisingTrackingStatus == FBSDKAdvertisingTrackingAllowed);
parameters[@"advertiser_tracking_enabled"] = @(allowed).stringValue;
}
parameters[@"application_tracking_enabled"] = @(!FBSDKSettings.limitEventAndDataUsage).stringValue;
NSString *userID = [FBSDKAppEvents userID];
if (userID) {
parameters[@"app_user_id"] = userID;
}
NSString *userData = [FBSDKAppEvents getUserData];
if (userData){
parameters[@"ud"] = userData;
}
[FBSDKAppEventsDeviceInfo extendDictionaryWithDeviceInfo:parameters];
static dispatch_once_t fetchBundleOnce;
static NSMutableArray *urlSchemes;
dispatch_once(&fetchBundleOnce, ^{
NSBundle *mainBundle = [NSBundle mainBundle];
urlSchemes = [[NSMutableArray alloc] init];
for (NSDictionary<NSString *, id> *fields in [mainBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"]) {
NSArray<NSString *> *schemesForType = fields[@"CFBundleURLSchemes"];
if (schemesForType) {
[urlSchemes addObjectsFromArray:schemesForType];
}
}
});
if (urlSchemes.count > 0) {
parameters[@"url_schemes"] = [FBSDKInternalUtility JSONStringForObject:urlSchemes error:NULL invalidObjectHandler:NULL];
}
return parameters;
}
+ (NSString *)advertiserID
{
if (![[FBSDKSettings advertiserIDCollectionEnabled] boolValue]) {
return nil;
}
NSString *result = nil;
Class ASIdentifierManagerClass = fbsdkdfl_ASIdentifierManagerClass();
if ([ASIdentifierManagerClass class]) {
ASIdentifierManager *manager = [ASIdentifierManagerClass sharedManager];
result = manager.advertisingIdentifier.UUIDString;
}
return result;
}
+ (FBSDKAdvertisingTrackingStatus)advertisingTrackingStatus
{
static dispatch_once_t fetchAdvertisingTrackingStatusOnce;
static FBSDKAdvertisingTrackingStatus status;
dispatch_once(&fetchAdvertisingTrackingStatusOnce, ^{
status = FBSDKAdvertisingTrackingUnspecified;
Class ASIdentifierManagerClass = fbsdkdfl_ASIdentifierManagerClass();
if ([ASIdentifierManagerClass class]) {
ASIdentifierManager *manager = [ASIdentifierManagerClass sharedManager];
if (manager) {
status = manager.advertisingTrackingEnabled ? FBSDKAdvertisingTrackingAllowed : FBSDKAdvertisingTrackingDisallowed;
}
}
});
return status;
}
+ (NSString *)anonymousID
{
// Grab previously written anonymous ID and, if none have been generated, create and
// persist a new one which will remain associated with this app.
NSString *result = [[self class] retrievePersistedAnonymousID];
if (!result) {
// Generate a new anonymous ID. Create as a UUID, but then prepend the fairly
// arbitrary 'XZ' to the front so it's easily distinguishable from IDFA's which
// will only contain hex.
result = [NSString stringWithFormat:@"XZ%@", [NSUUID UUID].UUIDString];
[self persistAnonymousID:result];
}
return result;
}
+ (NSString *)attributionID
{
#if TARGET_OS_TV
return nil;
#else
return [UIPasteboard pasteboardWithName:@"fb_app_attribution" create:NO].string;
#endif
}
// for tests only.
+ (void)clearLibraryFiles
{
[[NSFileManager defaultManager] removeItemAtPath:[[self class] persistenceFilePath:FBSDK_APPEVENTSUTILITY_ANONYMOUSIDFILENAME]
error:NULL];
[[NSFileManager defaultManager] removeItemAtPath:[[self class] persistenceFilePath:FBSDKTimeSpentFilename]
error:NULL];
}
+ (void)ensureOnMainThread:(NSString *)methodName className:(NSString *)className
{
FBSDKConditionalLog([NSThread isMainThread],
FBSDKLoggingBehaviorDeveloperErrors,
@"*** <%@, %@> is not called on the main thread. This can lead to errors.",
methodName,
className);
}
+ (NSString *)flushReasonToString:(FBSDKAppEventsFlushReason)flushReason
{
NSString *result = @"Unknown";
switch (flushReason) {
case FBSDKAppEventsFlushReasonExplicit:
result = @"Explicit";
break;
case FBSDKAppEventsFlushReasonTimer:
result = @"Timer";
break;
case FBSDKAppEventsFlushReasonSessionChange:
result = @"SessionChange";
break;
case FBSDKAppEventsFlushReasonPersistedEvents:
result = @"PersistedEvents";
break;
case FBSDKAppEventsFlushReasonEventThreshold:
result = @"EventCountThreshold";
break;
case FBSDKAppEventsFlushReasonEagerlyFlushingEvent:
result = @"EagerlyFlushingEvent";
break;
}
return result;
}
+ (void)logAndNotify:(NSString *)msg
{
[[self class] logAndNotify:msg allowLogAsDeveloperError:YES];
}
+ (void)logAndNotify:(NSString *)msg allowLogAsDeveloperError:(BOOL)allowLogAsDeveloperError
{
NSString *behaviorToLog = FBSDKLoggingBehaviorAppEvents;
if (allowLogAsDeveloperError) {
if ([FBSDKSettings.loggingBehaviors containsObject:FBSDKLoggingBehaviorDeveloperErrors]) {
// Rather than log twice, prefer 'DeveloperErrors' if it's set over AppEvents.
behaviorToLog = FBSDKLoggingBehaviorDeveloperErrors;
}
}
[FBSDKLogger singleShotLogEntry:behaviorToLog logEntry:msg];
NSError *error = [NSError fbErrorWithCode:FBSDKErrorAppEventsFlush message:msg];
[[NSNotificationCenter defaultCenter] postNotificationName:FBSDKAppEventsLoggingResultNotification object:error];
}
+ (BOOL)matchString:(NSString *)string
firstCharacterSet:(NSCharacterSet *)firstCharacterSet
restOfStringCharacterSet:(NSCharacterSet *)restOfStringCharacterSet
{
if (string.length == 0) {
return NO;
}
for (NSUInteger i = 0; i < string.length; i++) {
const unichar c = [string characterAtIndex:i];
if (i == 0) {
if (![firstCharacterSet characterIsMember:c]) {
return NO;
}
} else {
if (![restOfStringCharacterSet characterIsMember:c]) {
return NO;
}
}
}
return YES;
}
+ (BOOL)regexValidateIdentifier:(NSString *)identifier
{
static NSCharacterSet *firstCharacterSet;
static NSCharacterSet *restOfStringCharacterSet;
static dispatch_once_t onceToken;
static NSMutableSet *cachedIdentifiers;
dispatch_once(&onceToken, ^{
NSMutableCharacterSet *mutableSet = [NSMutableCharacterSet alphanumericCharacterSet];
[mutableSet addCharactersInString:@"_"];
firstCharacterSet = [mutableSet copy];
[mutableSet addCharactersInString:@"- "];
restOfStringCharacterSet = [mutableSet copy];
cachedIdentifiers = [[NSMutableSet alloc] init];
});
@synchronized(self) {
if (![cachedIdentifiers containsObject:identifier]) {
if ([self matchString:identifier
firstCharacterSet:firstCharacterSet
restOfStringCharacterSet:restOfStringCharacterSet]) {
[cachedIdentifiers addObject:identifier];
} else {
return NO;
}
}
}
return YES;
}
+ (BOOL)validateIdentifier:(NSString *)identifier
{
if (identifier == nil || identifier.length == 0 || identifier.length > FBSDK_APPEVENTSUTILITY_MAX_IDENTIFIER_LENGTH || ![[self class] regexValidateIdentifier:identifier]) {
[[self class] logAndNotify:[NSString stringWithFormat:@"Invalid identifier: '%@'. Must be between 1 and %d characters, and must be contain only alphanumerics, _, - or spaces, starting with alphanumeric or _.",
identifier, FBSDK_APPEVENTSUTILITY_MAX_IDENTIFIER_LENGTH]];
return NO;
}
return YES;
}
+ (void)persistAnonymousID:(NSString *)anonymousID
{
[[self class] ensureOnMainThread:NSStringFromSelector(_cmd) className:NSStringFromClass(self)];
NSDictionary *data = @{ FBSDK_APPEVENTSUTILITY_ANONYMOUSID_KEY : anonymousID };
NSString *content = [FBSDKInternalUtility JSONStringForObject:data error:NULL invalidObjectHandler:NULL];
[content writeToFile:[[self class] persistenceFilePath:FBSDK_APPEVENTSUTILITY_ANONYMOUSIDFILENAME]
atomically:YES
encoding:NSASCIIStringEncoding
error:nil];
}
+ (NSString *)persistenceFilePath:(NSString *)filename
{
NSSearchPathDirectory directory = NSLibraryDirectory;
NSArray *paths = NSSearchPathForDirectoriesInDomains(directory, NSUserDomainMask, YES);
NSString *docDirectory = paths[0];
return [docDirectory stringByAppendingPathComponent:filename];
}
+ (NSString *)retrievePersistedAnonymousID
{
[[self class] ensureOnMainThread:NSStringFromSelector(_cmd) className:NSStringFromClass(self)];
NSString *file = [[self class] persistenceFilePath:FBSDK_APPEVENTSUTILITY_ANONYMOUSIDFILENAME];
NSString *content = [[NSString alloc] initWithContentsOfFile:file
encoding:NSASCIIStringEncoding
error:nil];
NSDictionary *results = [FBSDKInternalUtility objectForJSONString:content error:NULL];
return results[FBSDK_APPEVENTSUTILITY_ANONYMOUSID_KEY];
}
// Given a candidate token (which may be nil), find the real token to string to use.
// Precedence: 1) provided token, 2) current token, 3) app | client token, 4) fully anonymous session.
+ (NSString *)tokenStringToUseFor:(FBSDKAccessToken *)token
{
if (!token) {
token = [FBSDKAccessToken currentAccessToken];
}
NSString *appID = [FBSDKAppEvents loggingOverrideAppID] ?: token.appID ?: [FBSDKSettings appID];
NSString *tokenString = token.tokenString;
if (!tokenString || ![appID isEqualToString:token.appID]) {
// If there's an logging override app id present, then we don't want to use the client token since the client token
// is intended to match up with the primary app id (and AppEvents doesn't require a client token).
NSString *clientTokenString = [FBSDKSettings clientToken];
if (clientTokenString && appID && [appID isEqualToString:token.appID]){
tokenString = [NSString stringWithFormat:@"%@|%@", appID, clientTokenString];
} else if (appID) {
tokenString = nil;
}
}
return tokenString;
}
+ (long)unixTimeNow
{
return (long)round([NSDate date].timeIntervalSince1970);
}
+ (id)getVariable:(NSString *)variableName fromInstance:(NSObject *)instance {
Ivar ivar = class_getInstanceVariable([instance class], variableName.UTF8String);
if (ivar != NULL) {
const char *encoding = ivar_getTypeEncoding(ivar);
if (encoding != NULL && encoding[0] == '@') {
return object_getIvar(instance, ivar);
}
}
return nil;
}
+ (NSNumber *)getNumberValue:(NSString *)text {
NSNumber *value = @0;
NSLocale *locale = [NSLocale currentLocale];
NSString *ds = [locale objectForKey:NSLocaleDecimalSeparator] ?: @".";
NSString *gs = [locale objectForKey:NSLocaleGroupingSeparator] ?: @",";
NSString *separators = [ds stringByAppendingString:gs];
NSString *regex = [NSString stringWithFormat:@"[+-]?([0-9]+[%1$@]?)?[%1$@]?([0-9]+[%1$@]?)+", separators];
NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:regex
options:0
error:nil];
NSTextCheckingResult *match = [re firstMatchInString:text
options:0
range:NSMakeRange(0, text.length)];
if (match) {
NSString *validText = [text substringWithRange:match.range];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.locale = locale;
formatter.numberStyle = NSNumberFormatterDecimalStyle;
value = [formatter numberFromString:validText];
if (nil == value) {
value = @(validText.floatValue);
}
}
return value;
}
+ (BOOL)isDebugBuild {
#if TARGET_IPHONE_SIMULATOR
return YES;
#else
BOOL isDevelopment = NO;
// There is no provisioning profile in AppStore Apps.
@try
{
NSData *data = [NSData dataWithContentsOfFile:[NSBundle.mainBundle pathForResource:@"embedded" ofType:@"mobileprovision"]];
if (data) {
const char *bytes = [data bytes];
NSMutableString *profile = [[NSMutableString alloc] initWithCapacity:data.length];
for (NSUInteger i = 0; i < data.length; i++) {
[profile appendFormat:@"%c", bytes[i]];
}
// Look for debug value, if detected we're in a development build.
NSString *cleared = [[profile componentsSeparatedByCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet] componentsJoinedByString:@""];
isDevelopment = ([cleared rangeOfString:@"<key>get-task-allow</key><true/>"].length > 0);
}
return isDevelopment;
}
@catch(NSException *exception)
{
}
return NO;
#endif
}
+ (BOOL)isSensitiveUserData:(NSString *)text
{
if (0 == text.length) {
return NO;
}
return [self isEmailAddress:text] || [self isCreditCardNumber:text];
}
+ (BOOL)isCreditCardNumber:(NSString *)text
{
text = [[text componentsSeparatedByCharactersInSet:[NSCharacterSet.decimalDigitCharacterSet invertedSet]] componentsJoinedByString:@""];
if (text.doubleValue == 0) {
return NO;
}
if (text.length < 9 || text.length > 21) {
return NO;
}
const char *chars = [text cStringUsingEncoding:NSUTF8StringEncoding];
if (NULL == chars) {
return NO;
}
BOOL isOdd = YES;
int oddSum = 0;
int evenSum = 0;
for (int i = (int)text.length - 1; i >= 0; i--) {
int digit = chars[i] - '0';
if (isOdd)
oddSum += digit;
else
evenSum += digit / 5 + (2 * digit) % 10;
isOdd = !isOdd;
}
return ((oddSum + evenSum) % 10 == 0);
}
+ (BOOL)isEmailAddress:(NSString *)text
{
NSString *pattern = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger matches = [regex numberOfMatchesInString:text options:0 range:NSMakeRange(0, [text length])];
return matches > 0;
}
@end