Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[iOS][Text Input] Avoid crash when its UIViewController.view is nil. #39768

Merged
merged 4 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2440,6 +2440,13 @@ - (void)addToInputParentViewIfNeeded:(FlutterTextInputView*)inputView {
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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,24 @@ - (FlutterTextRange*)getLineRangeFromTokenizer:(id<UITextInputTokenizer>)tokeniz

#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];
Expand Down