diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm index c19ea2a00c..ac201da0e5 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm @@ -312,6 +312,7 @@ static constexpr int kNumProfilerSamplesPerSec = 5; viewController ? [viewController getWeakPtr] : fml::WeakPtr(); self.iosPlatformView->SetOwnerViewController(_viewController); [self maybeSetupPlatformViewChannels]; + _textInputPlugin.get().viewController = viewController; if (viewController) { __block FlutterEngine* blockSelf = self; @@ -345,6 +346,7 @@ static constexpr int kNumProfilerSamplesPerSec = 5; - (void)notifyViewControllerDeallocated { [[self lifecycleChannel] sendMessage:@"AppLifecycleState.detached"]; + _textInputPlugin.get().viewController = nil; if (!_allowHeadlessExecution) { [self destroyContext]; } else { diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h index c17423f3a8..31c2071907 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h @@ -15,6 +15,7 @@ @interface FlutterTextInputPlugin : NSObject @property(nonatomic, assign) id textInputDelegate; +@property(nonatomic, assign) UIViewController* viewController; - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result; /** 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 ad583c49d1..b9a9bce520 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 @@ -1874,12 +1874,11 @@ static FlutterAutofillType autofillTypeOf(NSDictionary* configuration) { } // The UIView to add FlutterTextInputViews to. -- (UIView*)keyWindow { - UIWindow* keyWindow = [UIApplication sharedApplication].keyWindow; - NSAssert(keyWindow != nullptr, - @"The application must have a key window since the keyboard client " +- (UIView*)hostView { + NSAssert(self.viewController.view != nullptr, + @"The application must have a HostView since the keyboard client " @"must be part of the responder chain to function"); - return keyWindow; + return self.viewController.view; } // The UIView to add FlutterTextInputViews to. @@ -1952,7 +1951,7 @@ static FlutterAutofillType autofillTypeOf(NSDictionary* configuration) { if (![inputView isDescendantOfView:_inputHider]) { [_inputHider addSubview:inputView]; } - UIView* parentView = self.keyWindow; + 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 0112938145..cd803f7fb7 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 @@ -9,9 +9,14 @@ #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h" #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterEngine.h" +#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h" FLUTTER_ASSERT_ARC +@interface FlutterEngine () +- (nonnull FlutterTextInputPlugin*)textInputPlugin; +@end + @interface FlutterTextInputView () @property(nonatomic, copy) NSString* autofillId; - (void)setEditableTransform:(NSArray*)matrix; @@ -58,6 +63,8 @@ FLUTTER_ASSERT_ARC clearText:(BOOL)clearText delayRemoval:(BOOL)delayRemoval; - (NSArray*)textInputViews; +- (UIView*)hostView; +- (void)addToInputParentViewIfNeeded:(FlutterTextInputView*)inputView; @end @interface FlutterTextInputPluginTest : XCTestCase @@ -68,6 +75,7 @@ FLUTTER_ASSERT_ARC NSDictionary* _passwordTemplate; id engine; FlutterTextInputPlugin* textInputPlugin; + FlutterViewController* viewController; } - (void)setUp { @@ -76,6 +84,8 @@ FLUTTER_ASSERT_ARC engine = OCMClassMock([FlutterEngine class]); textInputPlugin = [[FlutterTextInputPlugin alloc] init]; textInputPlugin.textInputDelegate = engine; + viewController = [FlutterViewController new]; + textInputPlugin.viewController = viewController; } - (void)tearDown { @@ -87,6 +97,7 @@ FLUTTER_ASSERT_ARC [textInputPlugin cleanUpViewHierarchy:YES clearText:YES delayRemoval:NO]; [[[[textInputPlugin textInputView] superview] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; + viewController = nil; [super tearDown]; } @@ -1345,8 +1356,11 @@ FLUTTER_ASSERT_ARC } - (void)testFlutterTextInputPluginRetainsFlutterTextInputView { + FlutterViewController* flutterViewController = [FlutterViewController new]; FlutterTextInputPlugin* myInputPlugin = [[FlutterTextInputPlugin alloc] init]; myInputPlugin.textInputDelegate = engine; + myInputPlugin.viewController = flutterViewController; + __weak UIView* activeView; @autoreleasepool { FlutterMethodCall* setClientCall = [FlutterMethodCall @@ -1369,4 +1383,19 @@ FLUTTER_ASSERT_ARC XCTAssertNotNil(activeView); } +- (void)testFlutterTextInputPluginHostViewNilCrash { + FlutterTextInputPlugin* myInputPlugin = [[FlutterTextInputPlugin alloc] init]; + myInputPlugin.viewController = nil; + XCTAssertThrows([myInputPlugin hostView], @"Throws exception if host view is nil"); +} + +- (void)testFlutterTextInputPluginHostViewNotNil { + FlutterViewController* flutterViewController = [FlutterViewController new]; + FlutterEngine* flutterEngine = [[FlutterEngine alloc] init]; + [flutterEngine runWithEntrypoint:nil]; + flutterEngine.viewController = flutterViewController; + XCTAssertNotNil(flutterEngine.textInputPlugin.viewController); + XCTAssertNotNil([flutterEngine.textInputPlugin hostView]); +} + @end