Skip to content

Commit

Permalink
[iOS] Fixes system navigator pop not work when App's root vc is TabBa…
Browse files Browse the repository at this point in the history
…rController (flutter#30939)

* [iOS] Fixes system navigator pop not work when App's root vc is TabBarController

* Add test

* Update review feedback
  • Loading branch information
zhongwuzw authored Jan 25, 2022
1 parent 84e8e5d commit 54e1f3f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,15 @@ - (void)popSystemNavigator:(BOOL)isAnimated {
// 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<UIViewController*>([_engine.get() viewController]);
if (engineViewController != viewController) {
UIViewController* rootViewController =
[UIApplication sharedApplication].keyWindow.rootViewController;
if (engineViewController != rootViewController) {
[engineViewController dismissViewControllerAnimated:isAnimated completion:nil];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -48,4 +49,32 @@ - (void)testHasStrings {
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<fml::WeakPtrFactory<FlutterEngine>> _weakFactory =
std::make_unique<fml::WeakPtrFactory<FlutterEngine>>(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

0 comments on commit 54e1f3f

Please sign in to comment.