diff --git a/CleverTapSDK/CTInAppDisplayViewController.m b/CleverTapSDK/CTInAppDisplayViewController.m index 2c699215..d714f5c8 100644 --- a/CleverTapSDK/CTInAppDisplayViewController.m +++ b/CleverTapSDK/CTInAppDisplayViewController.m @@ -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(); }]; @@ -216,7 +229,6 @@ - (void)hideFromWindow:(BOOL)animated { } } - #pragma mark - CTInAppPassThroughViewDelegate - (void)viewWillPassThroughTouch { diff --git a/CleverTapSDK/CTInAppDisplayViewControllerPrivate.h b/CleverTapSDK/CTInAppDisplayViewControllerPrivate.h index 6cb54f76..3d09ffba 100644 --- a/CleverTapSDK/CTInAppDisplayViewControllerPrivate.h +++ b/CleverTapSDK/CTInAppDisplayViewControllerPrivate.h @@ -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; diff --git a/CleverTapSDK/InApps/CTInAppHTMLViewController.m b/CleverTapSDK/InApps/CTInAppHTMLViewController.m index a0d36943..fcdb4859 100644 --- a/CleverTapSDK/InApps/CTInAppHTMLViewController.m +++ b/CleverTapSDK/InApps/CTInAppHTMLViewController.m @@ -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 { @@ -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