Skip to content

Commit 5347c7e

Browse files
authored
fix(ios): secureWithoutAutofill on TextField (#11129)
1 parent ae6ca00 commit 5347c7e

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

apps/toolbox/src/pages/forms.xml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,27 @@
55
</Page.actionBar>
66
<ScrollView>
77
<StackLayout padding="20">
8-
<Label text="TextField:" fontWeight="bold" />
8+
<Label text="TextField secureWithoutAutofill" fontWeight="bold" />
9+
<TextField marginTop="6" backgroundColor="#efefef" padding="8" fontSize="18" secure="true" secureWithoutAutofill="true">
10+
</TextField>
11+
12+
<Label text="TextField" fontWeight="bold" marginTop="12" />
913
<TextField textChange="{{ textChange }}" marginTop="6" backgroundColor="#efefef" padding="8" fontSize="18" keyboardType="url" />
1014

1115
<Label text="{{ textInput }}" marginTop="6" />
1216

13-
<Label text="SSN Formatted input:" fontWeight="bold" marginTop="12" />
17+
<Label text="SSN Formatted input" fontWeight="bold" marginTop="12" />
1418
<TextField hint="XXX-XX-XXXX" style.placeholderColor="silver" textChange="{{textChangeSSN}}" keyboardType="number" color="black" width="80%" borderColor="silver" borderWidth="1" height="40" textAlignment="center" borderRadius="4" valueFormatter="{{valueFormatterSSN}}" padding="5" maxLength="11">
1519
</TextField>
1620
<Label text="{{ formattedSSNInput }}" marginTop="6" />
1721

18-
<Label text="Phone Formatted input:" fontWeight="bold" marginTop="12" />
22+
<Label text="Phone Formatted input" fontWeight="bold" marginTop="12" />
1923
<TextField hint="XXX-XXX-XXXX" style.placeholderColor="silver" textChange="{{textChangePhone}}" keyboardType="number" color="black" width="80%" borderColor="silver" borderWidth="1" height="40" textAlignment="center" borderRadius="4" valueFormatter="{{valueFormatterPhone}}" padding="5" maxLength="12">
2024
</TextField>
2125
<Label text="{{ formattedPhoneInput }}" marginTop="6" />
2226

2327
<Label text="TextView" fontWeight="bold" marginTop="12" />
24-
<TextView hint="Type text..." style.placeholderColor="silver" textChange="{{textChangeArea}}" color="black" width="80%" borderColor="silver" borderWidth="1" height="200" borderRadius="4" fontSize="14">
28+
<TextView hint="Type text..." style.placeholderColor="silver" textChange="{{textChangeArea}}" color="black" width="80%" borderColor="silver" borderWidth="1" height="125" borderRadius="4" fontSize="14">
2529
</TextView>
2630

2731
<Label text="TextField white-space + text-overflow" fontWeight="bold" marginTop="12" />

packages/core/ui/text-field/index.ios.ts

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class UITextFieldDelegateImpl extends NSObject implements UITextFieldDelegate {
1313
public static ObjCProtocols = [UITextFieldDelegate];
1414

1515
private _owner: WeakRef<TextField>;
16-
private firstEdit: boolean;
1716

1817
public static initWithOwner(owner: WeakRef<TextField>): UITextFieldDelegateImpl {
1918
const delegate = <UITextFieldDelegateImpl>UITextFieldDelegateImpl.new();
@@ -125,6 +124,7 @@ export class TextField extends TextFieldBase {
125124
super.initNativeView();
126125
this._delegate = UITextFieldDelegateImpl.initWithOwner(new WeakRef(this));
127126
this.nativeViewProtected.delegate = this._delegate;
127+
this._applySecureWithoutAutofillTraits(this.nativeViewProtected);
128128
}
129129

130130
disposeNativeView() {
@@ -140,6 +140,7 @@ export class TextField extends TextFieldBase {
140140

141141
public textFieldShouldBeginEditing(textField: UITextField): boolean {
142142
this._firstEdit = true;
143+
this._applySecureWithoutAutofillTraits(textField);
143144

144145
return this.editable;
145146
}
@@ -174,14 +175,7 @@ export class TextField extends TextFieldBase {
174175
}
175176

176177
public textFieldShouldChangeCharactersInRangeReplacementString(textField: UITextField, range: NSRange, replacementString: string): boolean {
177-
if (this.secureWithoutAutofill && !textField.secureTextEntry) {
178-
/**
179-
* Helps avoid iOS 12+ autofill strong password suggestion prompt
180-
* Discussed in several circles but for example:
181-
* https://github.com/expo/expo/issues/2571#issuecomment-473347380
182-
*/
183-
textField.secureTextEntry = true;
184-
}
178+
this._applySecureWithoutAutofillTraits(textField);
185179
const delta = replacementString.length - range.length;
186180
if (delta > 0) {
187181
if (textField.text.length + delta > this.maxLength) {
@@ -243,6 +237,44 @@ export class TextField extends TextFieldBase {
243237
}
244238
[secureProperty.setNative](value: boolean) {
245239
this.nativeTextViewProtected.secureTextEntry = value;
240+
this._applySecureWithoutAutofillTraits(this.nativeTextViewProtected);
241+
}
242+
243+
private _applySecureWithoutAutofillTraits(textField: UITextField): void {
244+
if (!textField || !this.secureWithoutAutofill) {
245+
return;
246+
}
247+
248+
const nativeField = textField as any;
249+
250+
textField.secureTextEntry = true;
251+
252+
if (nativeField.textContentType !== undefined) {
253+
nativeField.textContentType = typeof UITextContentTypeOneTimeCode !== 'undefined' ? UITextContentTypeOneTimeCode : '';
254+
}
255+
256+
if (nativeField.autocorrectionType !== undefined) {
257+
nativeField.autocorrectionType = UITextAutocorrectionType.No;
258+
}
259+
if (nativeField.spellCheckingType !== undefined) {
260+
nativeField.spellCheckingType = UITextSpellCheckingType.No;
261+
}
262+
if (nativeField.smartDashesType !== undefined) {
263+
nativeField.smartDashesType = UITextSmartDashesType.No;
264+
}
265+
if (nativeField.smartQuotesType !== undefined) {
266+
nativeField.smartQuotesType = UITextSmartQuotesType.No;
267+
}
268+
if (nativeField.smartInsertDeleteType !== undefined) {
269+
nativeField.smartInsertDeleteType = UITextSmartInsertDeleteType.No;
270+
}
271+
if (nativeField.passwordRules !== undefined) {
272+
nativeField.passwordRules = null;
273+
}
274+
275+
if (nativeField.reloadInputViews) {
276+
nativeField.reloadInputViews();
277+
}
246278
}
247279

248280
[colorProperty.getDefault](): { textColor: UIColor; tintColor: UIColor } {

0 commit comments

Comments
 (0)