Skip to content

Commit

Permalink
fix(deprecation warnings): Fix deprecation warnings (#301)
Browse files Browse the repository at this point in the history
* fix(deprecations): Add `+ (UIWindow *)getKeyWindow;` wrapper to AMPUtils

* fix(deprecations): Add diagnostic ignored around `statusBarFrame` usage

* fix(deprecations): Add availability-checked unarchive method to Amplitude

* fix(deprecations): Add availability checking to archive method in Amplitude

* fix(deprecations): Wrap usage of `CC_MD5` into `diagnostic ignored`

As mentioned by @haoliu-amp in
#250 (comment),
> 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.

* fix(deprecations): Add tvOS to availability checks

* style: Add missing newlines, replace dot syntax by bracket syntax
  • Loading branch information
JanNash authored Oct 20, 2020
1 parent f501c6c commit e7b0e6e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Sources/Amplitude/AMPEventExplorer.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)]];
Expand All @@ -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];
Expand Down
1 change: 1 addition & 0 deletions Sources/Amplitude/AMPUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#if TARGET_OS_IOS || TARGET_OS_MACCATALYST
+ (NSInteger)barBottomOffset;
+ (CGFloat)statusBarHeight;
+ (UIWindow *)getKeyWindow;
#endif

@end
28 changes: 27 additions & 1 deletion Sources/Amplitude/AMPUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,38 @@ + (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 {
#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);
}

+ (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
54 changes: 52 additions & 2 deletions Sources/Amplitude/Amplitude.m
Original file line number Diff line number Diff line change
Expand Up @@ -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<CC_MD5_DIGEST_LENGTH; i++) {
Expand Down Expand Up @@ -1647,7 +1655,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;
Expand Down Expand Up @@ -1677,8 +1685,50 @@ - (id)unarchive:(NSString*)path {
return nil;
}

- (id)unarchive:(NSData *)data error:(NSError **)error {
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 or tvOS's >= 11.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];
if (@available(tvOS 11.0, 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 or tvOS's >= 11.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 {
Expand Down

0 comments on commit e7b0e6e

Please sign in to comment.