iOS 11 disable password autofill accessory view option?
Solution 1:
iOS 11 & 12 & 13 - Swift 4.2 & 5 (Updated):
if #available(iOS 12, *) {
// iOS 12 & 13: Not the best solution, but it works.
passwordTextField.textContentType = .oneTimeCode
} else {
// iOS 11: Disables the autofill accessory view.
// For more information see the explanation below.
emailTextField.textContentType = .init(rawValue: "")
passwordTextField.textContentType = .init(rawValue: "")
}
iOS 11 explanation:
Make sure you setup all of your UITextField
objects like this.
If you have for example an UITextField
object where the user must enter his email address and another one where the user must enter his password assign UITextContentType("")
to both of their textContentType
property. Otherwise it will not work and the autoFill accessory view will still be shown.
Solution 2:
iOS 12 seems to recognise password textFields also by isSecureTextEntry
property and not just by textContentType
property, so making this accessory view disappear is not really possible unless you both set textContentType to nothing, and remove the secureEntry feature (and cause a security flaw in your app) which then prevents iOS 12 to recognise the textField as a password textField and show this annoying accessory view.
In my case the accessory caused a bug which made my app unresponsive when tapped (Which also got my app rejected in app review process). So I had to remove this feature. I didn't want to give on up this security feature so I had to solve things by my self.
The idea is to remove the secureEntry feature but add it by yourself manually. It did worked:
It can be done like that:
Swift 4 way:
First, as answered here, set textContentType
to nothing:
if #available(iOS 10.0, *) {
passwordText.textContentType = UITextContentType("")
emailText.textContentType = UITextContentType("")
}
Than, declare a String variable which will later contain our textField real content:
var passwordValue = ""
Add a target to the passwordTextField, which will be called each time the textField content changes:
passwordText.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
Now That's what will do the magic, declare the function that will handle the text replacements:
@objc func textFieldDidChange(_ textField: UITextField) {
if textField.text!.count > 1 {
// User did copy & paste
if passwordValue.count == 0 { // Pasted into an empty textField
passwordValue = String(textField.text!)
} else { // Pasted to a non empty textField
passwordValue += textField.text!.substring(from: passwordValue.count)
}
} else {
// User did input by keypad
if textField.text!.count > passwordValue.count { // Added chars
passwordValue += String(textField.text!.last!)
} else if textField.text!.count < passwordValue.count { // Removed chars
passwordValue = String(passwordValue.dropLast())
}
}
self.passwordText.text = String(repeating: "•", count: self.passwordText.text!.count)
}
Finally, Set textField's autocorrectionType
to .no
to remove predictive text:
passwordText.autocorrectionType = .no
That's it, use passwordValue
to perform your login.
Hope it'll help someone.
UPDATE
It catches pasted values also, forgot to add it before.
Solution 3:
The feature can be disabled by specifying a content type that is neither username nor password. For example, if the user should enter an email address, you could use
usernameTextField?.textContentType = .emailAddress
Solution 4:
in response to @Gal Shahar Answer.
iOS 12 recognise password textFields by isSecureTextEntry
property and not just by textContentType
property.
Way Around to bypass Auto-fill Suggestion.
- set
isSecureTextEntry
property to false.
self.passwordTextField.secureTextEntry = NO;
- Add a UITextField Delegate Method and enable the
isSecureTextEntry
property.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField == self.passwordTextField && !self.passwordTextField.secureTextEntry) {
self.passwordTextField.secureTextEntry = YES;
}
return YES;
}
NOTE: -
Do NOT Use shouldBeginEditing
UITextField delegate method it Will Still show Auto-filling Suggestion.
Do NOT Use textFieldDidChange
UITextField delegate method it Will Auto-delete the first charachter as the it Will happen after the first charachter is displayed. And 'secureTextEntry' will empty the field.