Skip to content

Commit 5f26cf6

Browse files
authored
Use weak reference check for controller hide (#379)
1 parent 73dbdca commit 5f26cf6

File tree

3 files changed

+27
-30
lines changed

3 files changed

+27
-30
lines changed

CleverTapSDK/CTInAppDisplayViewController.m

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,30 @@ - (void)showFromWindow:(BOOL)animated {
196196
}
197197

198198
- (void)hideFromWindow:(BOOL)animated {
199+
[self hideFromWindow:animated withCompletion:nil];
200+
}
201+
202+
- (void)hideFromWindow:(BOOL)animated withCompletion:(void (^)(void))completion {
203+
__weak typeof(self) weakSelf = self;
199204
void (^completionBlock)(void) = ^ {
200-
[self.window removeFromSuperview];
201-
self.window = nil;
202-
if (self.delegate && [self.delegate respondsToSelector:@selector(notificationDidDismiss:fromViewController:)]) {
203-
[self.delegate notificationDidDismiss:self.notification fromViewController:self];
205+
if (!weakSelf) {
206+
return;
207+
}
208+
if (weakSelf.window) {
209+
[weakSelf.window removeFromSuperview];
210+
weakSelf.window = nil;
211+
}
212+
if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(notificationDidDismiss:fromViewController:)]) {
213+
[weakSelf.delegate notificationDidDismiss:weakSelf.notification fromViewController:weakSelf];
214+
}
215+
if (completion) {
216+
completion();
204217
}
205218
};
206219

207220
if (animated) {
208221
[UIView animateWithDuration:0.25 animations:^{
209-
self.window.alpha = 0;
222+
weakSelf.window.alpha = 0;
210223
} completion:^(BOOL finished) {
211224
completionBlock();
212225
}];
@@ -216,7 +229,6 @@ - (void)hideFromWindow:(BOOL)animated {
216229
}
217230
}
218231

219-
220232
#pragma mark - CTInAppPassThroughViewDelegate
221233

222234
- (void)viewWillPassThroughTouch {

CleverTapSDK/CTInAppDisplayViewControllerPrivate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
- (void)showFromWindow:(BOOL)animated;
2424
- (void)hideFromWindow:(BOOL)animated;
25+
- (void)hideFromWindow:(BOOL)animated withCompletion:(void (^)(void))completion;
2526

2627
- (void)tappedDismiss;
2728
- (void)buttonTapped:(UIButton*)button;

CleverTapSDK/InApps/CTInAppHTMLViewController.m

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -510,29 +510,6 @@ - (void)showFromWindow:(BOOL)animated {
510510
}
511511
}
512512

513-
- (void)hideFromWindow:(BOOL)animated {
514-
void (^completionBlock)(void) = ^ {
515-
[self->webView.configuration.userContentController removeScriptMessageHandlerForName:@"clevertap"];
516-
[self.window removeFromSuperview];
517-
self.window = nil;
518-
if (self.delegate && [self.delegate respondsToSelector:@selector(notificationDidDismiss:fromViewController:)]) {
519-
[self.delegate notificationDidDismiss:self.notification fromViewController:self];
520-
}
521-
};
522-
523-
if (animated) {
524-
[UIView animateWithDuration:0.25 animations:^{
525-
self.window.alpha = 0;
526-
} completion:^(BOOL finished) {
527-
completionBlock();
528-
}];
529-
}
530-
else {
531-
completionBlock();
532-
}
533-
}
534-
535-
536513
#pragma mark - Public
537514

538515
- (void)show:(BOOL)animated {
@@ -541,7 +518,14 @@ - (void)show:(BOOL)animated {
541518
}
542519

543520
- (void)hide:(BOOL)animated {
544-
[self hideFromWindow:animated];
521+
__weak typeof(self) weakSelf = self;
522+
[self hideFromWindow:animated withCompletion:^{
523+
__strong typeof(weakSelf) strongSelf = weakSelf;
524+
if (!strongSelf) {
525+
return;
526+
}
527+
[strongSelf->webView.configuration.userContentController removeScriptMessageHandlerForName:@"clevertap"];
528+
}];
545529
}
546530

547531
@end

0 commit comments

Comments
 (0)