diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm index 88143fffde..3e81a39f2c 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm @@ -37,6 +37,8 @@ using namespace flutter; @implementation FlutterPlatformPlugin { fml::WeakPtr _engine; + // Used to detect whether this device has live text input ability or not. + UITextField* _textField; } - (instancetype)initWithEngine:(fml::WeakPtr)engine { @@ -88,6 +90,8 @@ using namespace flutter; result(nil); } else if ([method isEqualToString:@"Clipboard.hasStrings"]) { result([self clipboardHasStrings]); + } else if ([method isEqualToString:@"LiveText.isLiveTextInputAvailable"]) { + result(@([self isLiveTextInputAvailable])); } else { result(FlutterMethodNotImplemented); } @@ -277,4 +281,19 @@ using namespace flutter; return @{@"value" : @([UIPasteboard generalPasteboard].hasStrings)}; } +- (BOOL)isLiveTextInputAvailable { + return [[self textField] canPerformAction:@selector(captureTextFromCamera:) withSender:nil]; +} + +- (UITextField*)textField { + if (_textField == nil) { + _textField = [[UITextField alloc] init]; + } + return _textField; +} + +- (void)dealloc { + [_textField release]; + [super dealloc]; +} @end diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPluginTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPluginTest.mm index f5fbdf3352..94553aaf1f 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPluginTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPluginTest.mm @@ -14,6 +14,10 @@ @interface FlutterPlatformPluginTest : XCTestCase @end +@interface FlutterPlatformPlugin () +- (BOOL)isLiveTextInputAvailable; +@end + @implementation FlutterPlatformPluginTest - (void)testClipboardHasCorrectStrings { @@ -111,4 +115,24 @@ OCMVerify([navigationControllerMock popViewControllerAnimated:YES]); } +- (void)testWhetherDeviceHasLiveTextInputInvokeCorrectly { + FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil]; + std::unique_ptr> _weakFactory = + std::make_unique>(engine); + XCTestExpectation* invokeExpectation = + [self expectationWithDescription:@"isLiveTextInputAvailableInvoke"]; + FlutterPlatformPlugin* plugin = + [[FlutterPlatformPlugin alloc] initWithEngine:_weakFactory->GetWeakPtr()]; + FlutterPlatformPlugin* mockPlugin = OCMPartialMock(plugin); + FlutterMethodCall* methodCall = + [FlutterMethodCall methodCallWithMethodName:@"LiveText.isLiveTextInputAvailable" + arguments:nil]; + FlutterResult result = ^(id result) { + OCMVerify([mockPlugin isLiveTextInputAvailable]); + [invokeExpectation fulfill]; + }; + [mockPlugin handleMethodCall:methodCall result:result]; + [self waitForExpectationsWithTimeout:1 handler:nil]; +} + @end 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 779717793c..8a3e39d7a8 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 @@ -49,6 +49,7 @@ static NSString* const kSetEditableSizeAndTransformMethod = static NSString* const kSetMarkedTextRectMethod = @"TextInput.setMarkedTextRect"; static NSString* const kFinishAutofillContextMethod = @"TextInput.finishAutofillContext"; static NSString* const kSetSelectionRectsMethod = @"TextInput.setSelectionRects"; +static NSString* const kStartLiveTextInputMethod = @"TextInput.startLiveTextInput"; #pragma mark - TextInputConfiguration Field Names static NSString* const kSecureTextEntry = @"obscureText"; @@ -2098,6 +2099,9 @@ static BOOL IsSelectionRectCloserToPoint(CGPoint point, } else if ([method isEqualToString:kSetSelectionRectsMethod]) { [self setSelectionRects:args]; result(nil); + } else if ([method isEqualToString:kStartLiveTextInputMethod]) { + [self startLiveTextInput]; + result(nil); } else { result(FlutterMethodNotImplemented); } @@ -2142,6 +2146,15 @@ static BOOL IsSelectionRectCloserToPoint(CGPoint point, _activeView.selectionRects = rectsAsRect; } +- (void)startLiveTextInput { + if (@available(iOS 15.0, *)) { + if (_activeView == nil || !_activeView.isFirstResponder) { + return; + } + [_activeView captureTextFromCamera:nil]; + } +} + - (void)showTextInput { _activeView.viewResponder = _viewResponder; [self addToInputParentViewIfNeeded:_activeView]; 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 bec7028df6..41ed193411 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 @@ -68,6 +68,7 @@ FLUTTER_ASSERT_ARC - (UIView*)hostView; - (fml::WeakPtr)getWeakPtr; - (void)addToInputParentViewIfNeeded:(FlutterTextInputView*)inputView; +- (void)startLiveTextInput; @end @interface FlutterTextInputPluginTest : XCTestCase @@ -165,6 +166,17 @@ FLUTTER_ASSERT_ARC } #pragma mark - Tests + +- (void)testInvokeStartLiveTextInput { + FlutterMethodCall* methodCall = + [FlutterMethodCall methodCallWithMethodName:@"TextInput.startLiveTextInput" arguments:nil]; + FlutterTextInputPlugin* mockPlugin = OCMPartialMock(textInputPlugin); + [mockPlugin handleMethodCall:methodCall + result:^(id _Nullable result){ + }]; + OCMVerify([mockPlugin startLiveTextInput]); +} + - (void)testNoDanglingEnginePointer { __weak FlutterTextInputPlugin* weakFlutterTextInputPlugin; FlutterViewController* flutterViewController = [[FlutterViewController alloc] init];