Skip to content

Commit

Permalink
Use weak reference check for controller hide
Browse files Browse the repository at this point in the history
  • Loading branch information
nzagorchev committed Nov 5, 2024
1 parent 73dbdca commit 9af343f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
24 changes: 18 additions & 6 deletions CleverTapSDK/CTInAppDisplayViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,30 @@ - (void)showFromWindow:(BOOL)animated {
}

- (void)hideFromWindow:(BOOL)animated {
[self hideFromWindow:animated withCompletion:nil];
}

- (void)hideFromWindow:(BOOL)animated withCompletion:(void (^)(void))completion {
__weak typeof(self) weakSelf = self;
void (^completionBlock)(void) = ^ {
[self.window removeFromSuperview];
self.window = nil;
if (self.delegate && [self.delegate respondsToSelector:@selector(notificationDidDismiss:fromViewController:)]) {
[self.delegate notificationDidDismiss:self.notification fromViewController:self];
if (!weakSelf) {
return;
}
if (weakSelf.window) {
[weakSelf.window removeFromSuperview];
weakSelf.window = nil;
}
if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(notificationDidDismiss:fromViewController:)]) {
[weakSelf.delegate notificationDidDismiss:weakSelf.notification fromViewController:weakSelf];
}
if (completion) {
completion();
}
};

if (animated) {
[UIView animateWithDuration:0.25 animations:^{
self.window.alpha = 0;
weakSelf.window.alpha = 0;
} completion:^(BOOL finished) {
completionBlock();
}];
Expand All @@ -216,7 +229,6 @@ - (void)hideFromWindow:(BOOL)animated {
}
}


#pragma mark - CTInAppPassThroughViewDelegate

- (void)viewWillPassThroughTouch {
Expand Down
1 change: 1 addition & 0 deletions CleverTapSDK/CTInAppDisplayViewControllerPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

- (void)showFromWindow:(BOOL)animated;
- (void)hideFromWindow:(BOOL)animated;
- (void)hideFromWindow:(BOOL)animated withCompletion:(void (^)(void))completion;

- (void)tappedDismiss;
- (void)buttonTapped:(UIButton*)button;
Expand Down
32 changes: 8 additions & 24 deletions CleverTapSDK/InApps/CTInAppHTMLViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -510,29 +510,6 @@ - (void)showFromWindow:(BOOL)animated {
}
}

- (void)hideFromWindow:(BOOL)animated {
void (^completionBlock)(void) = ^ {
[self->webView.configuration.userContentController removeScriptMessageHandlerForName:@"clevertap"];
[self.window removeFromSuperview];
self.window = nil;
if (self.delegate && [self.delegate respondsToSelector:@selector(notificationDidDismiss:fromViewController:)]) {
[self.delegate notificationDidDismiss:self.notification fromViewController:self];
}
};

if (animated) {
[UIView animateWithDuration:0.25 animations:^{
self.window.alpha = 0;
} completion:^(BOOL finished) {
completionBlock();
}];
}
else {
completionBlock();
}
}


#pragma mark - Public

- (void)show:(BOOL)animated {
Expand All @@ -541,7 +518,14 @@ - (void)show:(BOOL)animated {
}

- (void)hide:(BOOL)animated {
[self hideFromWindow:animated];
__weak typeof(self) weakSelf = self;
[self hideFromWindow:animated withCompletion:^{
__strong typeof(weakSelf) strongSelf = weakSelf;
if (!strongSelf) {
return;
}
[strongSelf->webView.configuration.userContentController removeScriptMessageHandlerForName:@"clevertap"];
}];
}

@end

0 comments on commit 9af343f

Please sign in to comment.