From 7d62bf9aefc39339c915e57d24f38d23ea4d51ce Mon Sep 17 00:00:00 2001 From: luckysmg <2539699336@qq.com> Date: Thu, 23 Feb 2023 10:46:06 +0800 Subject: [PATCH] [iOS][Text Input] Avoid crash when its UIViewController.view is nil. (flutter/engine#39768) * [iOS] Avoid handle method calls when host UIViewController.view is nil. * Modify code. * ++ --- .../framework/Source/FlutterTextInputPlugin.mm | 7 +++++++ .../Source/FlutterTextInputPluginTest.mm | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm index 68b57ae22f..1ac316e819 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm @@ -2440,6 +2440,13 @@ static BOOL IsSelectionRectCloserToPoint(CGPoint point, if (![inputView isDescendantOfView:_inputHider]) { [_inputHider addSubview:inputView]; } + + if (_viewController.view == nil) { + // If view controller's view has detached from flutter engine, we don't add _inputHider + // in parent view to fallback and avoid crash. + // https://github.com/flutter/flutter/issues/106404. + return; + } UIView* parentView = self.hostView; if (_inputHider.superview != parentView) { [parentView addSubview:_inputHider]; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm index 459a74335b..84827c4722 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm @@ -166,6 +166,24 @@ FLUTTER_ASSERT_ARC #pragma mark - Tests +- (void)testWillNotCrashWhenViewControllerIsNil { + FlutterEngine* flutterEngine = [[FlutterEngine alloc] init]; + FlutterTextInputPlugin* inputPlugin = + [[FlutterTextInputPlugin alloc] initWithDelegate:(id)flutterEngine]; + XCTAssertNil(inputPlugin.viewController); + FlutterMethodCall* methodCall = [FlutterMethodCall methodCallWithMethodName:@"TextInput.show" + arguments:nil]; + XCTestExpectation* expectation = [[XCTestExpectation alloc] initWithDescription:@"result called"]; + + [inputPlugin handleMethodCall:methodCall + result:^(id _Nullable result) { + XCTAssertNil(result); + [expectation fulfill]; + }]; + XCTAssertNil(inputPlugin.activeView); + [self waitForExpectations:@[ expectation ] timeout:1.0]; +} + - (void)testInvokeStartLiveTextInput { FlutterMethodCall* methodCall = [FlutterMethodCall methodCallWithMethodName:@"TextInput.startLiveTextInput" arguments:nil];