@@ -88,11 +88,18 @@ static UIReturnKeyType ToUIReturnKeyType(NSString* inputType) {
8888 return UIReturnKeyDefault;
8989}
9090
91+ // TODO(LongCatIsLooong): add translation for evey predefined
92+ // UITextContentType.
93+ static UITextContentType ToUITextContentType (NSArray <NSString *>* hints) {
94+ if (hints == nil || hints.count == 0 )
95+ return @" " ;
96+
97+ return hints[0 ];
98+ }
99+
91100static NSString * _uniqueIdFromDictionary (NSDictionary * dictionary) {
92101 NSDictionary * autofill = dictionary[@" autofill" ];
93- if (autofill == nil )
94- return nil ;
95- return autofill[@" uniqueIdentifier" ];
102+ return autofill == nil ? nil : autofill[@" uniqueIdentifier" ];
96103}
97104
98105#pragma mark - FlutterTextPosition
@@ -147,34 +154,8 @@ - (id)copyWithZone:(NSZone*)zone {
147154
148155@end
149156
150- @interface FlutterTextInputView : UIView <UITextInput>
151-
152- // UITextInput
153- @property (nonatomic , readonly ) NSMutableString * text;
154- @property (nonatomic , readonly ) NSMutableString * markedText;
155- @property (readwrite , copy ) UITextRange* selectedTextRange;
156- @property (nonatomic , strong ) UITextRange* markedTextRange;
157- @property (nonatomic , copy ) NSDictionary * markedTextStyle;
158- @property (nonatomic , assign ) id <UITextInputDelegate> inputDelegate;
159-
160- // UITextInputTraits
161- @property (nonatomic ) UITextAutocapitalizationType autocapitalizationType;
162- @property (nonatomic ) UITextAutocorrectionType autocorrectionType;
163- @property (nonatomic ) UITextSpellCheckingType spellCheckingType;
164- @property (nonatomic ) BOOL enablesReturnKeyAutomatically;
165- @property (nonatomic ) UIKeyboardAppearance keyboardAppearance;
166- @property (nonatomic ) UIKeyboardType keyboardType;
167- @property (nonatomic ) UIReturnKeyType returnKeyType;
168- @property (nonatomic , getter =isSecureTextEntry) BOOL secureTextEntry;
169- @property (nonatomic ) UITextSmartQuotesType smartQuotesType API_AVAILABLE (ios(11.0 ));
170- @property (nonatomic ) UITextSmartDashesType smartDashesType API_AVAILABLE (ios(11.0 ));
171-
172- @property (nonatomic , assign ) id <FlutterTextInputDelegate> textInputDelegate;
173-
174- @end
175-
176157@implementation FlutterTextInputView {
177- NSString * _uniqueIdentifier ;
158+ NSString * _autofillId ;
178159 int _textInputClient;
179160 const char * _selectionAffinity;
180161 FlutterTextRange* _selectedTextRange;
@@ -218,15 +199,18 @@ - (void)dealloc {
218199 [_markedTextRange release ];
219200 [_selectedTextRange release ];
220201 [_tokenizer release ];
221- if (_uniqueIdentifier != nil )
222- [_uniqueIdentifier release ];
202+ [_autofillId release ];
223203 [super dealloc ];
224204}
225205
226206- (void )setTextInputClient : (int )client {
227207 _textInputClient = client;
228208}
229209
210+ - (void )setAutofillId : (NSString *)autofillId {
211+ _autofillId = [autofillId copy ];
212+ }
213+
230214- (void )setTextInputState : (NSDictionary *)state {
231215 NSString * newText = state[@" text" ];
232216 BOOL textChanged = ![self .text isEqualToString: newText];
@@ -637,10 +621,8 @@ - (void)updateEditingState {
637621 @" text" : [NSString stringWithString: self .text],
638622 };
639623
640- if (_textInputClient == 0 && _uniqueIdentifier != nil )
641- [_textInputDelegate updateEditingClient: _textInputClient
642- withState: state
643- withTag: _uniqueIdentifier];
624+ if (_textInputClient == 0 && _autofillId != nil )
625+ [_textInputDelegate updateEditingClient: _textInputClient withState: state withTag: _autofillId];
644626 else
645627 [_textInputDelegate updateEditingClient: _textInputClient withState: state];
646628}
@@ -810,9 +792,13 @@ - (void)setTextInputClient:(int)client withConfiguration:(NSDictionary*)configur
810792
811793 for (NSDictionary * field in allFields) {
812794 FlutterTextInputView* newInputView = [[FlutterTextInputView alloc ] init ];
795+ newInputView.textInputDelegate = _textInputDelegate;
813796 [_inputViews addObject: newInputView];
814797
815- if ([clientUniqueId isEqualToString: _uniqueIdFromDictionary (field)])
798+ NSString * autofillId = _uniqueIdFromDictionary (field);
799+ [newInputView setAutofillId: autofillId];
800+
801+ if ([clientUniqueId isEqualToString: autofillId])
816802 _activeView = newInputView;
817803
818804 [FlutterTextInputPlugin setupInputView: newInputView WithConfiguration: field];
@@ -858,14 +844,17 @@ + (void)setupInputView:(FlutterTextInputView*)inputView
858844 inputView.autocorrectionType = autocorrect && ![autocorrect boolValue ]
859845 ? UITextAutocorrectionTypeNo
860846 : UITextAutocorrectionTypeDefault;
861- if (autofill == nil ) {
862- if (@ available (iOS 10.0 , *))
847+ if (@ available (iOS 10.0 , *) ) {
848+ if (autofill == nil ) {
863849 inputView.textContentType = @" " ;
864- } else {
865- if (@available (iOS 10.0 , *))
866- inputView.textContentType = autofill[@" hints" ];
867-
868- [inputView setTextInputState: autofill[@" editingValue" ]];
850+ } else {
851+ inputView.textContentType = ToUITextContentType (autofill[@" hints" ]);
852+ [inputView setTextInputState: autofill[@" editingValue" ]];
853+ // An input field needs to be visible in order to get
854+ // autofilled when it's not the one that triggered
855+ // autofill.
856+ inputView.frame = CGRectMake (0 , 0 , 1 , 1 );
857+ }
869858 }
870859}
871860
@@ -876,4 +865,5 @@ - (void)setTextInputEditingState:(NSDictionary*)state {
876865- (void )clearTextInputClient {
877866 [_activeView setTextInputClient: 0 ];
878867}
868+
879869@end
0 commit comments