diff --git a/doric-iOS/Devkit/Classes/DoricDev.h b/doric-iOS/Devkit/Classes/DoricDev.h index ff13a1c1..308e4973 100644 --- a/doric-iOS/Devkit/Classes/DoricDev.h +++ b/doric-iOS/Devkit/Classes/DoricDev.h @@ -45,6 +45,8 @@ NS_ASSUME_NONNULL_BEGIN - (void)openDevMode; +- (void)openDevMode:(UIViewController *)vc; + - (void)closeDevMode; - (BOOL)isInDevMode; @@ -72,6 +74,9 @@ NS_ASSUME_NONNULL_BEGIN - (void)addStatusCallback:(id )callback; - (void)removeStatusCallback:(id )callback; + +UIViewController* _Nonnull findBestViewController(UIViewController* _Nonnull vc); + @end NS_ASSUME_NONNULL_END diff --git a/doric-iOS/Devkit/Classes/DoricDev.m b/doric-iOS/Devkit/Classes/DoricDev.m index ddf827ee..80f9a9d5 100644 --- a/doric-iOS/Devkit/Classes/DoricDev.m +++ b/doric-iOS/Devkit/Classes/DoricDev.m @@ -27,6 +27,7 @@ #import "DoricDebugDriver.h" #import "DoricDevViewController.h" #import "DoricDevMonitor.h" +#import "DoricUtil.h" @interface DoricContextDebuggable : NSObject @property(nonatomic, weak) DoricContext *doricContext; @@ -104,6 +105,19 @@ - (void)openDevMode { [navigationController pushViewController:devViewController animated:NO]; } +- (void)openDevMode:(UIViewController *)vc { + DoricDevViewController *devViewController = [DoricDevViewController new]; + + UIViewController *viewController = findBestViewController(vc); + UINavigationController *navigationController; + if ([viewController isKindOfClass:[UINavigationController class]]) { + navigationController = (UINavigationController *) viewController; + } else { + navigationController = viewController.navigationController; + } + [navigationController pushViewController:devViewController animated:NO]; +} + - (void)closeDevMode { [self stopDebugging:YES]; if (self.wsClient) { @@ -259,4 +273,36 @@ - (NSString *)ip { stringByReplacingOccurrencesOfString:@":7777" withString:@""]; } + +UIViewController* _Nonnull findBestViewController(UIViewController* _Nonnull vc) { + if (vc.presentedViewController && ![vc.presentedViewController isKindOfClass:[UIAlertController class]]) { + // Return presented view controller + return findBestViewController(vc.presentedViewController); + } else if ([vc isKindOfClass:[UISplitViewController class]]) { + // Return right hand side + UISplitViewController* svc = (UISplitViewController*) vc; + if (svc.viewControllers.count > 0) + return findBestViewController(svc.viewControllers.lastObject); + else + return vc; + } else if ([vc isKindOfClass:[UINavigationController class]]) { + // Return top view + UINavigationController* svc = (UINavigationController*) vc; + if (svc.viewControllers.count > 0) + return findBestViewController(svc.topViewController); + else + return vc; + } else if ([vc isKindOfClass:[UITabBarController class]]) { + // Return visible view + UITabBarController* svc = (UITabBarController*) vc; + if (svc.viewControllers.count > 0) + return findBestViewController(svc.selectedViewController); + else + return vc; + } else { + // Unknown view controller type, return last child view controller + return vc; + } +} + @end diff --git a/doric-iOS/Example/Example/ViewController.m b/doric-iOS/Example/Example/ViewController.m index 7047cd51..767b6d44 100644 --- a/doric-iOS/Example/Example/ViewController.m +++ b/doric-iOS/Example/Example/ViewController.m @@ -69,7 +69,7 @@ - (BOOL)isSimulator { - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 0) { - [[DoricDev instance] openDevMode]; + [[DoricDev instance] openDevMode:self]; return; } NSString *file = self.demoFilePaths[(NSUInteger) indexPath.row];