Skip to content

Commit c183238

Browse files
n8trE-B-Smith
authored andcommitted
Fixed a crash when the root view controller is UINavigationController and the user taps on a button that pushes a new view controller with 3 seconds timeframe when the BNCMatchView is added to primary window. Without this patch unloadViewController also removes the first view controller of the UINavigationController and eventually crashing the app. (#539)
1 parent 7425920 commit c183238

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

Branch-SDK/Branch-SDK/BNCStrongMatchHelper.m

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,13 @@ - (BOOL) willLoadViewControllerWithURL:(NSURL*)matchURL {
291291
self.matchView.alpha = 1.0;
292292
[self.matchView addSubview:self.matchViewController.view];
293293

294-
[self.primaryWindow.rootViewController addChildViewController:self.matchViewController];
295-
UIView *parentView = self.primaryWindow.rootViewController.view ?: self.primaryWindow;
294+
UIViewController *rootViewController = [self topViewController:self.primaryWindow.rootViewController];
295+
296+
[rootViewController addChildViewController:self.matchViewController];
297+
UIView *parentView = rootViewController.view ?: self.primaryWindow;
296298
[parentView insertSubview:self.matchView atIndex:0];
297299

298-
[self.matchViewController didMoveToParentViewController:self.primaryWindow.rootViewController];
300+
[self.matchViewController didMoveToParentViewController:rootViewController];
299301

300302
return YES;
301303
}
@@ -319,6 +321,25 @@ - (void) unloadViewController {
319321
self.requestInProgress = NO;
320322
}
321323

324+
/**
325+
Find the top view controller that is not of type UINavigationController or UITabBarController
326+
*/
327+
- (UIViewController *)topViewController:(UIViewController *)baseViewController {
328+
if ([baseViewController isKindOfClass:[UINavigationController class]]) {
329+
return [self topViewController: ((UINavigationController *)baseViewController).visibleViewController];
330+
}
331+
332+
if ([baseViewController isKindOfClass:[UITabBarController class]]) {
333+
return [self topViewController: ((UITabBarController *)baseViewController).selectedViewController];
334+
}
335+
336+
if ([baseViewController presentedViewController] != nil) {
337+
return [self topViewController: [baseViewController presentedViewController]];
338+
}
339+
340+
return baseViewController;
341+
}
342+
322343
- (void)safariViewController:(SFSafariViewController *)controller
323344
didCompleteInitialLoad:(BOOL)didLoadSuccessfully {
324345
NSLog(@"Safari Did load. Success: %d.", didLoadSuccessfully); // eDebug

0 commit comments

Comments
 (0)