[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.

* ++
This commit is contained in:
luckysmg
2023-02-23 10:46:06 +08:00
committed by GitHub
parent 080532b347
commit 7d62bf9aef
2 changed files with 25 additions and 0 deletions

View File

@@ -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];

View File

@@ -166,6 +166,24 @@ FLUTTER_ASSERT_ARC
#pragma mark - Tests
- (void)testWillNotCrashWhenViewControllerIsNil {
FlutterEngine* flutterEngine = [[FlutterEngine alloc] init];
FlutterTextInputPlugin* inputPlugin =
[[FlutterTextInputPlugin alloc] initWithDelegate:(id<FlutterTextInputDelegate>)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];