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.
textContentType
property to.textContentType
- This should tell iOS 11 that your fields aren't username/password fields (even though they are) and prevent the accessory view being displayed; Something like ` self.passwordField.textContentType = .textContentType` – Petulance