-
Notifications
You must be signed in to change notification settings - Fork 244
/
CountlyCommon.m
685 lines (550 loc) · 19.5 KB
/
CountlyCommon.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
// CountlyCommon.m
//
// This code is provided under the MIT License.
//
// Please visit www.count.ly for more information.
#import "CountlyCommon.h"
#include <CommonCrypto/CommonDigest.h>
NSString* const kCountlyReservedEventOrientation = @"[CLY]_orientation";
NSString* const kCountlyOrientationKeyMode = @"mode";
NSString* const kCountlyVisibility = @"cly_v";
@interface CountlyCommon ()
{
NSCalendar* gregorianCalendar;
NSTimeInterval startTime;
}
@property long long lastTimestamp;
#if (TARGET_OS_IOS || TARGET_OS_VISION )
@property (nonatomic) NSString* lastInterfaceOrientation;
#endif
#if (TARGET_OS_IOS || TARGET_OS_VISION || TARGET_OS_TV )
@property (nonatomic) UIBackgroundTaskIdentifier bgTask;
#endif
@end
NSString* const kCountlySDKVersion = @"24.7.9";
NSString* const kCountlySDKName = @"objc-native-ios";
NSString* const kCountlyErrorDomain = @"ly.count.ErrorDomain";
NSString* const kCountlyInternalLogPrefix = @"[Countly] ";
@implementation CountlyCommon
@synthesize lastTimestamp;
static CountlyCommon *s_sharedInstance = nil;
static dispatch_once_t onceToken;
+ (instancetype)sharedInstance
{
dispatch_once(&onceToken, ^{s_sharedInstance = self.new;});
return s_sharedInstance;
}
- (instancetype)init
{
if (self = [super init])
{
gregorianCalendar = [NSCalendar.alloc initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
startTime = NSDate.date.timeIntervalSince1970;
self.lastTimestamp = 0;
self.SDKVersion = kCountlySDKVersion;
self.SDKName = kCountlySDKName;
}
return self;
}
- (void)resetInstance {
CLY_LOG_I(@"%s", __FUNCTION__);
onceToken = 0;
s_sharedInstance = nil;
_hasStarted = false;
}
- (BOOL)hasStarted
{
if (!_hasStarted)
CountlyPrint(@"SDK should be started first!");
return _hasStarted;
}
//NOTE: This is an equivalent of hasStarted, but without internal logging.
- (BOOL)hasStarted_
{
return _hasStarted;
}
void CountlyInternalLog(CLYInternalLogLevel level, NSString *format, ...)
{
if (!CountlyCommon.sharedInstance.enableDebug && !CountlyCommon.sharedInstance.loggerDelegate)
return;
if (level > CountlyCommon.sharedInstance.internalLogLevel)
return;
va_list args;
va_start(args, format);
NSString* logString = [NSString.alloc initWithFormat:format arguments:args];
NSArray<NSString *> *logLevelPrefixes =
@[
@"None",
@"Error",
@"Warning",
@"Info",
@"Debug",
@"Verbose",
];
logString = [NSString stringWithFormat:@"[%@] %@", logLevelPrefixes[level], logString];
#if DEBUG
if (CountlyCommon.sharedInstance.enableDebug)
CountlyPrint(logString);
#endif
if ([CountlyCommon.sharedInstance.loggerDelegate respondsToSelector:@selector(internalLog:withLevel:)])
{
NSString* logStringWithPrefix = [NSString stringWithFormat:@"%@%@", kCountlyInternalLogPrefix, logString];
[CountlyCommon.sharedInstance.loggerDelegate internalLog:logStringWithPrefix withLevel:level];
}
va_end(args);
}
void CountlyPrint(NSString *stringToPrint)
{
NSLog(@"%@%@", kCountlyInternalLogPrefix, stringToPrint);
}
#pragma mark - Time/Date related methods
- (NSInteger)hourOfDay
{
NSDateComponents* components = [gregorianCalendar components:NSCalendarUnitHour fromDate:NSDate.date];
return components.hour;
}
- (NSInteger)dayOfWeek
{
NSDateComponents* components = [gregorianCalendar components:NSCalendarUnitWeekday fromDate:NSDate.date];
return components.weekday - 1;
}
- (NSInteger)timeZone
{
return NSTimeZone.systemTimeZone.secondsFromGMT / 60;
}
- (NSInteger)timeSinceLaunch
{
return (int)NSDate.date.timeIntervalSince1970 - startTime;
}
- (NSTimeInterval)uniqueTimestamp
{
long long now = floor(NSDate.date.timeIntervalSince1970 * 1000);
if (now <= self.lastTimestamp)
self.lastTimestamp++;
else
self.lastTimestamp = now;
return (NSTimeInterval)(self.lastTimestamp / 1000.0);
}
- (NSString *)randomEventID
{
const int size = 6;
void *randomBuffer = malloc(size);
arc4random_buf(randomBuffer, size);
NSData* randomData = [NSData dataWithBytesNoCopy:randomBuffer length:size freeWhenDone:YES];
NSString* randomBase64 = [randomData base64EncodedStringWithOptions:0];
NSTimeInterval timestamp = self.uniqueTimestamp;
NSString* randomEventID = [NSString stringWithFormat:@"%@%lld", randomBase64, (long long)(timestamp * 1000)];
return randomEventID;
}
#pragma mark - Orientation
- (void)observeDeviceOrientationChanges
{
#if (TARGET_OS_IOS)
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(deviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
#endif
}
- (void)deviceOrientationDidChange:(NSNotification *)notification
{
if (!self.enableOrientationTracking)
return;
//NOTE: Delay is needed for interface orientation change animation to complete. Otherwise old interface orientation value is returned.
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(recordOrientation) object:nil];
[self performSelector:@selector(recordOrientation) withObject:nil afterDelay:0.5];
}
- (void)recordOrientation
{
#if (TARGET_OS_IOS)
if (!self.enableOrientationTracking)
return;
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
CLY_LOG_W(@"%s App is in the background, 'Record Orientation' will be ignored", __FUNCTION__);
return;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
UIInterfaceOrientation interfaceOrientation = UIApplication.sharedApplication.statusBarOrientation;
#pragma GCC diagnostic pop
NSString* mode = nil;
if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
mode = @"portrait";
else if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
mode = @"landscape";
if (!mode)
{
CLY_LOG_D(@"Interface orientation is not landscape or portrait.");
return;
}
if ([mode isEqualToString:self.lastInterfaceOrientation])
{
CLY_LOG_V(@"Interface orientation is still same: %@", self.lastInterfaceOrientation);
return;
}
CLY_LOG_D(@"Interface orientation is now: %@", mode);
if (!CountlyConsentManager.sharedInstance.consentForUserDetails)
return;
self.lastInterfaceOrientation = mode;
[Countly.sharedInstance recordReservedEvent:kCountlyReservedEventOrientation segmentation:@{kCountlyOrientationKeyMode: mode}];
#endif
}
#pragma mark - Others
- (void)startBackgroundTask
{
#if (TARGET_OS_IOS || TARGET_OS_VISION || TARGET_OS_TV)
if (self.bgTask != UIBackgroundTaskInvalid)
return;
self.bgTask = [UIApplication.sharedApplication beginBackgroundTaskWithExpirationHandler:^
{
[UIApplication.sharedApplication endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
}];
#endif
}
- (void)finishBackgroundTask
{
#if (TARGET_OS_IOS || TARGET_OS_VISION || TARGET_OS_TV)
if (self.bgTask != UIBackgroundTaskInvalid && !CountlyConnectionManager.sharedInstance.connection)
{
[UIApplication.sharedApplication endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
}
#endif
}
#if (TARGET_OS_IOS || TARGET_OS_TV)
- (UIViewController *)topViewController
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
UIViewController* topVC = UIApplication.sharedApplication.keyWindow.rootViewController;
#pragma GCC diagnostic pop
while (YES)
{
if (topVC.presentedViewController)
topVC = topVC.presentedViewController;
else if ([topVC isKindOfClass:UINavigationController.class])
topVC = ((UINavigationController *)topVC).topViewController;
else if ([topVC isKindOfClass:UITabBarController.class])
topVC = ((UITabBarController *)topVC).selectedViewController;
else
break;
}
return topVC;
}
- (void)tryPresentingViewController:(UIViewController *)viewController
{
[self tryPresentingViewController:viewController withCompletion:nil];
}
- (void)tryPresentingViewController:(UIViewController *)viewController withCompletion:(void (^ __nullable) (void))completion
{
UIViewController* topVC = self.topViewController;
if (topVC)
{
[topVC presentViewController:viewController animated:YES completion:^
{
if (completion)
completion();
}];
return;
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
{
[self tryPresentingViewController:viewController];
});
}
#endif
- (NSURLSession *)URLSession
{
if (CountlyConnectionManager.sharedInstance.URLSessionConfiguration)
{
return [NSURLSession sessionWithConfiguration:CountlyConnectionManager.sharedInstance.URLSessionConfiguration];
}
else
{
return NSURLSession.sharedSession;
}
}
@end
#pragma mark - Internal ViewController
#if (TARGET_OS_IOS)
@implementation CLYInternalViewController : UIViewController
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
if (self.webView)
{
CGRect frame = CGRectInset(self.view.bounds, 20.0, 20.0);
UIEdgeInsets insets = UIEdgeInsetsZero;
if (@available(iOS 11.0, *))
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
insets = UIApplication.sharedApplication.keyWindow.safeAreaInsets;
#pragma GCC diagnostic pop
}
self.webView.navigationDelegate = self;
frame = UIEdgeInsetsInsetRect(frame, insets);
self.webView.frame = frame;
}
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSString *url = navigationAction.request.URL.absoluteString;
if ([url containsString:@"cly_x_int=1"]) {
CLY_LOG_I(@"%s Opening url [%@] in external browser", __FUNCTION__, url);
[[UIApplication sharedApplication] openURL:navigationAction.request.URL options:@{} completionHandler:^(BOOL success) {
if (success) {
CLY_LOG_I(@"%s url [%@] opened in external browser", __FUNCTION__, url);
}
else {
CLY_LOG_I(@"%s unable to open url [%@] in external browser", __FUNCTION__, url);
}
}];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation
{
CLY_LOG_I(@"%s Web view has start loading", __FUNCTION__);
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
CLY_LOG_I(@"%s Web view has finished loading", __FUNCTION__);
}
@end
@implementation CLYButton : UIButton
const CGFloat kCountlyDismissButtonSize = 30.0;
const CGFloat kCountlyDismissButtonMargin = 10.0;
const CGFloat kCountlyDismissButtonStandardStatusBarHeight = 20.0;
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
[self addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)touchUpInside:(id)sender
{
if (self.onClick)
self.onClick(self);
}
+ (CLYButton *)dismissAlertButton:(NSString * _Nullable)closeButtonText
{
if (!closeButtonText) {
closeButtonText = @"x";
}
CLYButton* dismissButton = [CLYButton buttonWithType:UIButtonTypeCustom];
dismissButton.frame = (CGRect){CGPointZero, kCountlyDismissButtonSize, kCountlyDismissButtonSize};
[dismissButton setTitle:closeButtonText forState:UIControlStateNormal];
[dismissButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
dismissButton.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.5];
dismissButton.layer.cornerRadius = dismissButton.bounds.size.width * 0.5;
dismissButton.layer.borderColor = [UIColor.blackColor colorWithAlphaComponent:0.7].CGColor;
dismissButton.layer.borderWidth = 1.0;
dismissButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
return dismissButton;
}
+ (CLYButton *)dismissAlertButton
{
return [CLYButton dismissAlertButton:nil];
}
- (void)positionToTopRight
{
[self positionToTopRight:NO];
}
- (void)positionToTopRightConsideringStatusBar
{
[self positionToTopRight:YES];
}
- (void)positionToTopRight:(BOOL)shouldConsiderStatusBar
{
CGRect rect = self.frame;
rect.origin.x = self.superview.bounds.size.width - self.bounds.size.width - kCountlyDismissButtonMargin;
rect.origin.y = kCountlyDismissButtonMargin;
if (shouldConsiderStatusBar)
{
if (@available(iOS 11.0, *))
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
CGFloat top = UIApplication.sharedApplication.keyWindow.safeAreaInsets.top;
#pragma GCC diagnostic pop
if (top)
{
rect.origin.y += top;
}
else
{
rect.origin.y += kCountlyDismissButtonStandardStatusBarHeight;
}
}
else
{
rect.origin.y += kCountlyDismissButtonStandardStatusBarHeight;
}
}
self.frame = rect;
}
@end
#endif
#pragma mark - Proxy Object
@implementation CLYDelegateInterceptor
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
return [self.originalDelegate methodSignatureForSelector:sel];
}
- (void)forwardInvocation:(NSInvocation *)invocation
{
if ([self.originalDelegate respondsToSelector:invocation.selector])
[invocation invokeWithTarget:self.originalDelegate];
else
[super forwardInvocation:invocation];
}
@end
#pragma mark - Categories
NSString* CountlyJSONFromObject(id object)
{
if (!object)
return nil;
if (![NSJSONSerialization isValidJSONObject:object])
{
CLY_LOG_W(@"Object is not valid for converting to JSON!");
return nil;
}
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:object options:0 error:&error];
if (error)
{
CLY_LOG_W(@"%s, JSON can not be created error:[ %@ ]", __FUNCTION__, error);
}
return [data cly_stringUTF8];
}
@implementation NSString (Countly)
- (NSString *)cly_URLEscaped
{
NSCharacterSet* charset = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~"];
return [self stringByAddingPercentEncodingWithAllowedCharacters:charset];
}
- (NSString *)cly_SHA256
{
const char* s = [self UTF8String];
unsigned char digest[CC_SHA256_DIGEST_LENGTH];
CC_SHA256(s, (CC_LONG)strlen(s), digest);
NSMutableString* hash = NSMutableString.new;
for (int i = 0; i < CC_SHA256_DIGEST_LENGTH; i++)
[hash appendFormat:@"%02x", digest[i]];
return hash;
}
- (NSData *)cly_dataUTF8
{
return [self dataUsingEncoding:NSUTF8StringEncoding];
}
- (NSString *)cly_valueForQueryStringKey:(NSString *)key
{
NSString* tempURLString = [@"http://example.com/path?" stringByAppendingString:self];
NSURLComponents* URLComponents = [NSURLComponents componentsWithString:tempURLString];
for (NSURLQueryItem* queryItem in URLComponents.queryItems)
{
if ([queryItem.name isEqualToString:key])
{
return queryItem.value;
}
}
return nil;
}
- (NSString *)cly_truncatedKey:(NSString *)explanation
{
if (self.length > CountlyCommon.sharedInstance.maxKeyLength)
{
CLY_LOG_W(@"%@ length is more than the limit (%ld)! So, it will be truncated: %@.", explanation, (long)CountlyCommon.sharedInstance.maxKeyLength, self);
return [self substringToIndex:CountlyCommon.sharedInstance.maxKeyLength];
}
return self;
}
- (NSString *)cly_truncatedValue:(NSString *)explanation
{
if (self.length > CountlyCommon.sharedInstance.maxValueLength)
{
CLY_LOG_W(@"%@ length is more than the limit (%ld)! So, it will be truncated: %@.", explanation, (long)CountlyCommon.sharedInstance.maxValueLength, self);
return [self substringToIndex:CountlyCommon.sharedInstance.maxValueLength];
}
return self;
}
@end
@implementation NSArray (Countly)
- (NSString *)cly_JSONify
{
return [CountlyJSONFromObject(self) cly_URLEscaped];
}
- (NSArray *) cly_filterSupportedDataTypes {
NSMutableArray *filteredArray = [NSMutableArray array];
for (id obj in self) {
if ([obj isKindOfClass:[NSNumber class]] || [obj isKindOfClass:[NSString class]]) {
[filteredArray addObject:obj];
} else {
CLY_LOG_W(@"%s, Removed invalid type from array: %@", __FUNCTION__, [obj class]);
}
}
return filteredArray.copy;
}
@end
@implementation NSDictionary (Countly)
- (NSString *)cly_JSONify
{
return [CountlyJSONFromObject(self) cly_URLEscaped];
}
- (NSDictionary *)cly_truncated:(NSString *)explanation
{
NSMutableDictionary* truncatedDict = self.mutableCopy;
[self enumerateKeysAndObjectsUsingBlock:^(NSString * key, id obj, BOOL * stop)
{
NSString* truncatedKey = [key cly_truncatedKey:[explanation stringByAppendingString:@" key"]];
if (![truncatedKey isEqualToString:key])
{
truncatedDict[truncatedKey] = obj;
[truncatedDict removeObjectForKey:key];
}
if ([obj isKindOfClass:NSString.class])
{
NSString* truncatedValue = [obj cly_truncatedValue:[explanation stringByAppendingString:@" value"]];
if (![truncatedValue isEqualToString:obj])
{
truncatedDict[truncatedKey] = truncatedValue;
}
}
}];
return truncatedDict.copy;
}
- (NSDictionary *)cly_limited:(NSString *)explanation
{
NSArray* allKeys = self.allKeys;
if (allKeys.count <= CountlyCommon.sharedInstance.maxSegmentationValues)
return self;
NSMutableArray* excessKeys = allKeys.mutableCopy;
[excessKeys removeObjectsInRange:(NSRange){0, CountlyCommon.sharedInstance.maxSegmentationValues}];
CLY_LOG_W(@"%s, Number of key-value pairs in %@ is more than the limit (%ld)! So, some of them will be removed %@", __FUNCTION__, explanation, (long)CountlyCommon.sharedInstance.maxSegmentationValues, [excessKeys description]);
NSMutableDictionary* limitedDict = self.mutableCopy;
[limitedDict removeObjectsForKeys:excessKeys];
return limitedDict.copy;
}
- (NSMutableDictionary *) cly_filterSupportedDataTypes
{
NSMutableDictionary<NSString *, id> *filteredDictionary = [NSMutableDictionary dictionary];
for (NSString *key in self) {
id value = [self objectForKey:key];
if ([value isKindOfClass:[NSNumber class]] ||
[value isKindOfClass:[NSString class]] ||
([value isKindOfClass:[NSArray class]] && (value = [value cly_filterSupportedDataTypes]))) {
[filteredDictionary setObject:value forKey:key];
} else {
CLY_LOG_W(@"%s, Removed invalid type for key %@: %@", __FUNCTION__, key, [value class]);
}
}
return filteredDictionary.mutableCopy;
}
@end
@implementation NSData (Countly)
- (NSString *)cly_stringUTF8
{
return [NSString.alloc initWithData:self encoding:NSUTF8StringEncoding];
}
@end