From 16d0ee936876e943976770377e26c381bc801b52 Mon Sep 17 00:00:00 2001 From: Wu Zhong Date: Tue, 25 Jan 2022 12:35:43 +0800 Subject: [PATCH] [iOS] Fixes system navigator pop not work when App's root vc is TabBarController (flutter/engine#30939) * [iOS] Fixes system navigator pop not work when App's root vc is TabBarController * Add test * Update review feedback --- .../framework/Source/FlutterPlatformPlugin.mm | 13 +++++---- .../Source/FlutterPlatformPluginTest.mm | 29 +++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm index cba52ffdc4..257cb4420c 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm @@ -241,12 +241,15 @@ using namespace flutter; // in the navigation hierarchy. // It's also possible in an Add2App scenario that the FlutterViewController was presented // outside the context of a UINavigationController, and still wants to be popped. - UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController; - if ([viewController isKindOfClass:[UINavigationController class]]) { - [((UINavigationController*)viewController) popViewControllerAnimated:isAnimated]; + + UIViewController* engineViewController = [_engine.get() viewController]; + UINavigationController* navigationController = [engineViewController navigationController]; + if (navigationController) { + [navigationController popViewControllerAnimated:isAnimated]; } else { - auto engineViewController = static_cast([_engine.get() viewController]); - if (engineViewController != viewController) { + UIViewController* rootViewController = + [UIApplication sharedApplication].keyWindow.rootViewController; + if (engineViewController != rootViewController) { [engineViewController dismissViewControllerAnimated:isAnimated completion:nil]; } } diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPluginTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPluginTest.mm index 691d627c33..23c7d2f942 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPluginTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPluginTest.mm @@ -7,6 +7,7 @@ #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h" #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h" +#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.h" #import "flutter/shell/platform/darwin/ios/platform_view_ios.h" @@ -48,4 +49,32 @@ XCTAssertEqual(value, true); } +- (void)testPopSystemNavigator { + FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil]; + [engine runWithEntrypoint:nil]; + FlutterViewController* flutterViewController = + [[FlutterViewController alloc] initWithEngine:engine nibName:nil bundle:nil]; + UINavigationController* navigationController = + [[UINavigationController alloc] initWithRootViewController:flutterViewController]; + UITabBarController* tabBarController = [[UITabBarController alloc] init]; + tabBarController.viewControllers = @[ navigationController ]; + std::unique_ptr> _weakFactory = + std::make_unique>(engine); + FlutterPlatformPlugin* plugin = + [[FlutterPlatformPlugin alloc] initWithEngine:_weakFactory->GetWeakPtr()]; + + id navigationControllerMock = OCMPartialMock(navigationController); + OCMStub([navigationControllerMock popViewControllerAnimated:YES]); + // Set some string to the pasteboard. + __block bool calledSet = false; + FlutterResult resultSet = ^(id result) { + calledSet = true; + }; + FlutterMethodCall* methodCallSet = + [FlutterMethodCall methodCallWithMethodName:@"SystemNavigator.pop" arguments:@(YES)]; + [plugin handleMethodCall:methodCallSet result:resultSet]; + XCTAssertEqual(calledSet, true); + OCMVerify([navigationControllerMock popViewControllerAnimated:YES]); +} + @end