From 86cad78d940c5ae0b657c46b99837227dc615881 Mon Sep 17 00:00:00 2001 From: Jan Nash Date: Mon, 19 Oct 2020 21:59:18 +0200 Subject: [PATCH 1/7] fix(deprecations): Add `+ (UIWindow *)getKeyWindow;` wrapper to AMPUtils --- Sources/Amplitude/AMPEventExplorer.m | 4 ++-- Sources/Amplitude/AMPUtils.h | 1 + Sources/Amplitude/AMPUtils.m | 20 +++++++++++++++++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Sources/Amplitude/AMPEventExplorer.m b/Sources/Amplitude/AMPEventExplorer.m index 7ee0e057..2a05d38f 100644 --- a/Sources/Amplitude/AMPEventExplorer.m +++ b/Sources/Amplitude/AMPEventExplorer.m @@ -59,7 +59,7 @@ - (void)showBubbleView { dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void) { - [[[AMPUtils getSharedApplication] keyWindow] addSubview:self.bubbleView]; + [[AMPUtils getKeyWindow] addSubview:self.bubbleView]; }); [self.bubbleView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showInfoView)]]; @@ -70,7 +70,7 @@ - (void)showBubbleView { - (void)showInfoView { dispatch_async(dispatch_get_main_queue(), ^(void){ if (self.bubbleView != nil) { - UIViewController *rootViewController = [[[AMPUtils getSharedApplication] keyWindow] rootViewController]; + UIViewController *rootViewController = [[AMPUtils getKeyWindow] rootViewController]; NSBundle *bundle = [NSBundle bundleForClass:[AMPInfoViewController class]]; AMPInfoViewController *infoVC = [[AMPInfoViewController alloc] initWithNibName:@"AMPInfoViewController" bundle:bundle]; diff --git a/Sources/Amplitude/AMPUtils.h b/Sources/Amplitude/AMPUtils.h index 7670da64..24455777 100644 --- a/Sources/Amplitude/AMPUtils.h +++ b/Sources/Amplitude/AMPUtils.h @@ -41,6 +41,7 @@ #if TARGET_OS_IOS || TARGET_OS_MACCATALYST + (NSInteger)barBottomOffset; + (CGFloat)statusBarHeight; ++ (UIWindow *)getKeyWindow; #endif @end diff --git a/Sources/Amplitude/AMPUtils.m b/Sources/Amplitude/AMPUtils.m index 3cc75716..a3ced8c7 100644 --- a/Sources/Amplitude/AMPUtils.m +++ b/Sources/Amplitude/AMPUtils.m @@ -172,12 +172,30 @@ + (NSInteger)barBottomOffset { + (CGFloat)statusBarHeight { CGSize statusBarSize; if (@available(iOS 13.0, *)) { - statusBarSize = [[[[AMPUtils getSharedApplication].keyWindow windowScene] statusBarManager] statusBarFrame].size; + statusBarSize = [[[[AMPUtils getKeyWindow] windowScene] statusBarManager] statusBarFrame].size; } else { statusBarSize = [[AMPUtils getSharedApplication] statusBarFrame].size; } return MIN(statusBarSize.width, statusBarSize.height); } + ++ (UIWindow *)getKeyWindow { + if (@available(iOS 13.0, *)) { + for (UIWindow *window in [[AMPUtils getSharedApplication] windows]) { + if (window.isKeyWindow) { return window; } + } + return nil; + } else { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + // Even with the availability check above, Xcode would still emit a deprecation warning here. + // Since there's no way that it could be reached on iOS's >= 13.0 + // (where `[UIApplication keyWindow]` was deprecated), we simply ignore the warning. + return [[AMPUtils getSharedApplication] keyWindow]; +#pragma clang diagnostic pop + } +} + #endif @end From 86c866b0c1f77d4188cddedb02839d28d57d54b9 Mon Sep 17 00:00:00 2001 From: Jan Nash Date: Mon, 19 Oct 2020 22:03:57 +0200 Subject: [PATCH 2/7] fix(deprecations): Add diagnostic ignored around `statusBarFrame` usage --- Sources/Amplitude/AMPUtils.m | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sources/Amplitude/AMPUtils.m b/Sources/Amplitude/AMPUtils.m index a3ced8c7..5fa2feb2 100644 --- a/Sources/Amplitude/AMPUtils.m +++ b/Sources/Amplitude/AMPUtils.m @@ -174,7 +174,13 @@ + (CGFloat)statusBarHeight { if (@available(iOS 13.0, *)) { statusBarSize = [[[[AMPUtils getKeyWindow] windowScene] statusBarManager] statusBarFrame].size; } else { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + // Even with the availability check above, Xcode would still emit a deprecation warning here. + // Since there's no way that it could be reached on iOS's >= 13.0 + // (where `[UIApplication statusBarFrame]` was deprecated), we simply ignore the warning. statusBarSize = [[AMPUtils getSharedApplication] statusBarFrame].size; +#pragma clang diagnostic pop } return MIN(statusBarSize.width, statusBarSize.height); } From 61be52230e8c29c1c8be893c92d30f2a5b201fcd Mon Sep 17 00:00:00 2001 From: Jan Nash Date: Mon, 19 Oct 2020 22:07:28 +0200 Subject: [PATCH 3/7] fix(deprecations): Add availability-checked unarchive method to Amplitude --- Sources/Amplitude/Amplitude.m | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Sources/Amplitude/Amplitude.m b/Sources/Amplitude/Amplitude.m index ac95656b..382571cb 100644 --- a/Sources/Amplitude/Amplitude.m +++ b/Sources/Amplitude/Amplitude.m @@ -1647,7 +1647,7 @@ - (id)unarchive:(NSString*)path { NSData *inputData = [fileManager contentsAtPath:path]; NSError *error = nil; if (inputData != nil) { - id data = [NSKeyedUnarchiver unarchiveTopLevelObjectWithData:inputData error:&error]; + id data = [self unarchive:inputData error:&error]; if (error == nil) { if (data != nil) { return data; @@ -1677,6 +1677,21 @@ - (id)unarchive:(NSString*)path { return nil; } +- (id)unarchive:(NSData *)data error:(NSError **)error { + if (@available(iOS 12, *)) { + return [NSKeyedUnarchiver unarchivedObjectOfClass:[NSDictionary class] fromData:data error:error]; + } else { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + // Even with the availability check above, Xcode would still emit a deprecation warning here. + // Since there's no way that it could be reached on iOS's >= 12.0 + // (where `[NSKeyedUnarchiver unarchiveTopLevelObjectWithData:error:]` was deprecated), + // we simply ignore the warning. + return [NSKeyedUnarchiver unarchiveTopLevelObjectWithData:data error:error]; +#pragma clang diagnostic pop + } +} + - (BOOL)archive:(id) obj toFile:(NSString*)path { return [NSKeyedArchiver archiveRootObject:obj toFile:path]; } From c49f45e08463dfd38dadd9879eaac1f58273bd03 Mon Sep 17 00:00:00 2001 From: Jan Nash Date: Mon, 19 Oct 2020 22:25:24 +0200 Subject: [PATCH 4/7] fix(deprecations): Add availability checking to archive method in Amplitude --- Sources/Amplitude/Amplitude.m | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Sources/Amplitude/Amplitude.m b/Sources/Amplitude/Amplitude.m index 382571cb..2d16e288 100644 --- a/Sources/Amplitude/Amplitude.m +++ b/Sources/Amplitude/Amplitude.m @@ -1693,7 +1693,34 @@ - (id)unarchive:(NSData *)data error:(NSError **)error { } - (BOOL)archive:(id) obj toFile:(NSString*)path { - return [NSKeyedArchiver archiveRootObject:obj toFile:path]; + if (@available(iOS 12, *)) { + NSError *archiveError = nil; + NSData *data = [NSKeyedArchiver archivedDataWithRootObject:obj requiringSecureCoding:NO error:&archiveError]; + if (archiveError != nil) { + AMPLITUDE_ERROR(@"ERROR: Unable to archive object %@: %@", obj, archiveError); + return NO; + } + if (data == nil) { + AMPLITUDE_ERROR(@"ERROR: Archived data is nil for obj: %@", obj); + return NO; + } + NSError *writeError = nil; + BOOL writeSuccessful = [data writeToFile:path options:NSDataWritingAtomic error:&writeError]; + if (writeError != nil || !writeSuccessful) { + AMPLITUDE_ERROR(@"ERROR: Unable to write data to file for object %@: %@", obj, archiveError); + return NO; + } + return YES; + } else { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + // Even with the availability check above, Xcode would still emit a deprecation warning here. + // Since there's no way that this path could be reached on iOS's >= 12.0 + // (where `[NSKeyedArchiver archiveRootObject:toFile:]` was deprecated), + // we simply ignore the warning. + return [NSKeyedArchiver archiveRootObject:obj toFile:path]; +#pragma clang diagnostic pop + } } - (BOOL)moveFileIfNotExists:(NSString*)from to:(NSString*)to { From 524d504a5b7f64c6991f8bb1d3b1bab3d34c9f50 Mon Sep 17 00:00:00 2001 From: Jan Nash Date: Mon, 19 Oct 2020 22:33:03 +0200 Subject: [PATCH 5/7] fix(deprecations): Wrap usage of `CC_MD5` into `diagnostic ignored` As mentioned by @haoliu-amp in https://github.com/amplitude/Amplitude-iOS/issues/250#issuecomment-655224554, > This crypto algorithm is used for our checksum field, actually you don't need to worry about the security concern for that. > However, we will see if we wanna switch it to SHA256. Based on this, we can silence the compile warning here until a fix is implemented. --- Sources/Amplitude/Amplitude.m | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Sources/Amplitude/Amplitude.m b/Sources/Amplitude/Amplitude.m index 2d16e288..3d7218f1 100644 --- a/Sources/Amplitude/Amplitude.m +++ b/Sources/Amplitude/Amplitude.m @@ -1534,7 +1534,15 @@ - (BOOL)isArgument:(id) argument validType:(Class) class methodName:(NSString*)m - (NSString*)md5HexDigest:(NSString*)input { const char* str = [input UTF8String]; unsigned char result[CC_MD5_DIGEST_LENGTH]; + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + // As mentioned by @haoliu-amp in // https://github.com/amplitude/Amplitude-iOS/issues/250#issuecomment-655224554, + // > This crypto algorithm is used for our checksum field, actually you don't need to worry about the security concern for that. + // > However, we will see if we wanna switch it to SHA256. + // Based on this, we can silence the compile warning here until a fix is implemented. CC_MD5(str, (CC_LONG) strlen(str), result); +#pragma clang diagnostic pop NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2]; for(int i = 0; i Date: Mon, 19 Oct 2020 22:53:00 +0200 Subject: [PATCH 6/7] fix(deprecations): Add tvOS to availability checks --- Sources/Amplitude/Amplitude.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/Amplitude/Amplitude.m b/Sources/Amplitude/Amplitude.m index 3d7218f1..109c3a8a 100644 --- a/Sources/Amplitude/Amplitude.m +++ b/Sources/Amplitude/Amplitude.m @@ -1686,13 +1686,13 @@ - (id)unarchive:(NSString*)path { } - (id)unarchive:(NSData *)data error:(NSError **)error { - if (@available(iOS 12, *)) { + if (@available(iOS 12, tvOS 11.0, *)) { return [NSKeyedUnarchiver unarchivedObjectOfClass:[NSDictionary class] fromData:data error:error]; } else { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" // Even with the availability check above, Xcode would still emit a deprecation warning here. - // Since there's no way that it could be reached on iOS's >= 12.0 + // Since there's no way that it could be reached on iOS's >= 12.0 or tvOS's >= 11.0 // (where `[NSKeyedUnarchiver unarchiveTopLevelObjectWithData:error:]` was deprecated), // we simply ignore the warning. return [NSKeyedUnarchiver unarchiveTopLevelObjectWithData:data error:error]; @@ -1701,7 +1701,7 @@ - (id)unarchive:(NSData *)data error:(NSError **)error { } - (BOOL)archive:(id) obj toFile:(NSString*)path { - if (@available(iOS 12, *)) { + if (@available(tvOS 11.0, iOS 12, *)) { NSError *archiveError = nil; NSData *data = [NSKeyedArchiver archivedDataWithRootObject:obj requiringSecureCoding:NO error:&archiveError]; if (archiveError != nil) { @@ -1723,7 +1723,7 @@ - (BOOL)archive:(id) obj toFile:(NSString*)path { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" // Even with the availability check above, Xcode would still emit a deprecation warning here. - // Since there's no way that this path could be reached on iOS's >= 12.0 + // Since there's no way that this path could be reached on iOS's >= 12.0 or tvOS's >= 11.0 // (where `[NSKeyedArchiver archiveRootObject:toFile:]` was deprecated), // we simply ignore the warning. return [NSKeyedArchiver archiveRootObject:obj toFile:path]; From b4fb72993e844a9c849224e52e9a9664e85a58ce Mon Sep 17 00:00:00 2001 From: Jan Nash Date: Tue, 20 Oct 2020 00:25:26 +0200 Subject: [PATCH 7/7] style: Add missing newlines, replace dot syntax by bracket syntax --- Sources/Amplitude/AMPUtils.m | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sources/Amplitude/AMPUtils.m b/Sources/Amplitude/AMPUtils.m index 5fa2feb2..3e3fc14c 100644 --- a/Sources/Amplitude/AMPUtils.m +++ b/Sources/Amplitude/AMPUtils.m @@ -188,7 +188,9 @@ + (CGFloat)statusBarHeight { + (UIWindow *)getKeyWindow { if (@available(iOS 13.0, *)) { for (UIWindow *window in [[AMPUtils getSharedApplication] windows]) { - if (window.isKeyWindow) { return window; } + if ([window isKeyWindow]) { + return window; + } } return nil; } else {