You could try different answers here that conclude it's likely possible to remove the accessory view. But this leaves some bugs.
You could try to implement a custom keyboard perhaps, only for password fields. Also try to disable suggestions for your textfield, I think that also hides the accessoryView.
The code above does work, but if the users set up their Apple ID account, Then, the name of the apple id will be display on the keyboard you cannot disable it by set autocorrectionType to No, not sure if Apple still refines this autofill feature, it's quite buggy right now.
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
This turned off the key symbol for both the password field and the email field that preceded it, and in this way you don't use one of the predefined values and you avoid showing unrelated suggestions in the keyboard accessory view.
Autofill is by default enabled for users. iOS saves all passwords in the keychain and make them available in keyboard in your apps. UITextView and UITextField automatically considered for AutoFill password. you can disable by specifying a content type that is neither username nor password but if the content type info already stored in keychain it will show in the quick bar. so better to assign empty UITextContentType type and it will not show quickbar.
Example:
if #available(iOS 10.0, *) {
self.textField.textContentType = UITextContentType("")
} else {
// Fallback on earlier versions
}
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.
A very simple approach in ios11 worked for me. Suppose your iboutlets are usernametextfield and passwordtextfield. In viewDidLoad() function of your viewcontroller that holds the both outlest use the following code
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:
NOTE: try putting this code on the password, password confirm (if applicable), AND email textfields. I was not putting it on the email textfield and it was still popping up for the two password fields.
if #available(iOS 12, *) {
// iOS 12: Not the best solution, but it works.
cell.textField.textContentType = .oneTimeCode
} else {
// iOS 11: Disables the autofill accessory view.
cell.textField.textContentType = .init(rawValue: "")
}
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.
From what I can tell, Bem's answer does not work in iOS 12 and Gal Shahar's answer does not account for some edge cases (for example, if a user deletes multiple characters at once). I worked around this using an IBAction, thus removing the necessity to check for iOS version altogether. I'm just a beginner, so this might not be the "best" answer or the most efficient, but it made the most sense to me:
First, uncheck "Secure Text Entry" in the Storyboard or set it to "false"/"NO" via code for your password UITextField. This will prevent iOS from attempting to AutoFill.
Then, link your password UITextField to an IBAction. Mine is called on:
Editing did begin
Editing did change
Editing did end
The IBAction function I wrote determines the differences between a user's starting password and what has been inputed into the password UITextField and creates a new password based on this information:
class Login: UIViewController {
var password = ""
override func viewDidLoad() { super.viewDidLoad() }
@IBAction func editPasswordField(_ sender: UITextField) {
var input = Array(sender.text ?? "")
var oldPassword = Array(password)
var newPassword = Array("")
//if character(s) are simply deleted from "passwordField" (not replaced or added to), "cursorPosition" is used to determine which corresponding character(s) need to also be removed from "oldPassword"
//this is indicated by "input" comprising of only "•" (bullets) and being shorter in length than "oldPassword"
var onlyBullets = true
for char in input { if char != "•" { onlyBullets = false } }
if onlyBullets && input.count < oldPassword.count {
if let selectedRange = sender.selectedTextRange {
let cursorPosition = sender.offset(from: sender.beginningOfDocument, to: selectedRange.start)
let prefix = String(oldPassword.prefix(cursorPosition))
let suffix = String(oldPassword.suffix(input.count - cursorPosition))
input = Array(prefix + suffix)
} else { input = Array("") }
}
//if no changes were made via input, input would comprise solely of a number of bullets equal to the length of "oldPassword"
//therefore, the number of changes made to "oldPassword" via "input" can be measured with "bulletDifference" by calculating the number of characters in "input" that are NOT bullets
var bulletDifference = oldPassword.count
for char in input { if char == "•" { bulletDifference -= 1 } }
//the only way "bulletDifference" can be less than 0 is if a user copy-pasted a bullet into "input", which cannot be allowed because it breaks this function
//if a user pastes bullet(s) into "input", "input" is deleted
//an edge case not accounted for is pasting a mix of characters and bullets (i.e. "ex•mple") when "oldPassword.count" exceeds the number of bullets in the mixed input, but this does not cause crashes and therefore is not worth preventing
if bulletDifference < 0 {
bulletDifference = oldPassword.count
input = Array("")
}
//"bulletDifference" is used to remove every character from "oldPassword" that corresponds with a character in "input" that has been changed
//a changed character in "input" is indicated by the fact that it is not a bullet
//once "bulletDifference" equals the number of bullets deleted, this loop ends
var bulletsDeleted = 0
for i in 0..<input.count {
if bulletsDeleted == bulletDifference { break }
if input[i] != "•" {
oldPassword.remove(at: i - bulletsDeleted)
bulletsDeleted += 1
}
}
//what remains of "oldPassword" is used to substitute bullets in "input" for appropriate characters to create "newPassword"
//for example, if "oldPassword" is "AcbDE" and "input" is "•bc••", then "oldPassword" will get truncated to "ADE" and "newPassword" will equal "A" + "bc" + "DE", or "AbcDE"
var i = 0
for char in input {
if char == "•" {
newPassword.append(oldPassword[i])
i += 1
} else { newPassword.append(char) }
}
password = String(newPassword)
//"passwordField.text" is then converted into a string of bullets equal to the length of the new password to ensure password security in the UI
sender.text = String(repeating: "•", count: password.count)
}
}
I think set all UITextField textContentType in form to UITextContentType("") or .oneTimeCode is not a clean solution. Enable/Disable isSecureTextEntry still give you the same issue.
@Gal Shahar 's Answer is nice but it is still not perfect. The masked char is not the same as the masked char that used in secure entry text from apple. It should use Unicode Character 'BLACK CIRCLE' (U+25CF) https://www.fileformat.info/info/unicode/char/25cf/index.htm
Also, it is not handling cursor movement. It will change the cursor position to the end of the text when inserting text in the middle. It will give you the wrong value when selecting and replacing text.
When you decide to use custom isSecureEntryText to avoid autofill password, here is the code:
Swift 5 (simple version)
@IBOutlet weak var passwordTextField: UITextField!
var maskedPasswordChar: String = "●"
var passwordText: String = ""
var isSecureTextEntry: Bool = true {
didSet {
let selectedTextRange = passwordTextField.selectedTextRange
passwordTextField.text = isSecureTextEntry ? String(repeating: maskedPasswordChar, count: passwordText.count) : passwordText
passwordTextField.selectedTextRange = selectedTextRange
}
}
//this is UITextFieldDelegate
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == passwordTextField {
//update password string
if let swiftRange = Range(range, in: passwordText) {
passwordText = passwordText.replacingCharacters(in: swiftRange, with: string)
} else {
passwordText = string
}
//replace textField text with masked password char
textField.text = isSecureTextEntry ? String(repeating: maskedPasswordChar, count: passwordText.count) : passwordText
//handle cursor movement
if let newPosition = textField.position(from: textField.beginningOfDocument, offset: range.location + string.utf16.count) {
textField.selectedTextRange = textField.textRange(from: newPosition, to: newPosition)
}
return false
}
return true
}
Swift 5 (COMPLETE version with securing last char animation)
private struct Constants {
static let SecuringLastCharPasswordDelay = 1.5
}
@IBOutlet weak var passwordTextField: UITextField!
private var secureTextAnimationQueue: [String] = []
var maskedPasswordChar: String = "●"
var passwordText: String = ""
var isSecureTextEntry: Bool = true {
didSet {
secureTextAnimationQueue.removeAll()
let selectedTextRange = passwordTextField.selectedTextRange
passwordTextField.text = isSecureTextEntry ? String(repeating: maskedPasswordChar, count: passwordText.count) : passwordText
passwordTextField.selectedTextRange = selectedTextRange
}
}
//this is UITextFieldDelegate
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == passwordTextField {
//update password string
if let swiftRange = Range(range, in: passwordText) {
passwordText = passwordText.replacingCharacters(in: swiftRange, with: string)
} else {
passwordText = string
}
//replace textField text with masked password char
updateTextFieldString(textField, shouldChangeCharactersIn: range, replacementString: string)
//handle cursor movement
if let newPosition = textField.position(from: textField.beginningOfDocument, offset: range.location + string.utf16.count) {
textField.selectedTextRange = textField.textRange(from: newPosition, to: newPosition)
}
return false
}
return true
}
private func updateTextFieldString(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) {
if isSecureTextEntry {
if string.count == .one, let text = textField.text {
let maskedText = String(repeating: maskedPasswordChar, count: text.count)
var newMaskedText = String()
if let swiftRange = Range(range, in: maskedText) {
newMaskedText = maskedText.replacingCharacters(in: swiftRange, with: string)
} else {
newMaskedText = text + maskedText
}
textField.text = newMaskedText
secureTextAnimationQueue.append(string)
asyncWorker.asyncAfter(deadline: .now() + Constants.SecuringLastCharPasswordDelay) { [weak self] in
self?.securingLastPasswordChar()
}
} else {
secureTextAnimationQueue.removeAll()
textField.text = String(repeating: maskedPasswordChar, count: passwordText.count)
}
} else {
textField.text = passwordText
}
}
private func securingLastPasswordChar() {
guard secureTextAnimationQueue.count > .zero, isSecureTextEntry else { return }
secureTextAnimationQueue.removeFirst()
if secureTextAnimationQueue.count == .zero {
let selectedTextRange = passwordTextField.selectedTextRange
passwordTextField.text = String(repeating: maskedPasswordChar, count: passwordText.count)
passwordTextField.selectedTextRange = selectedTextRange
}
}
I have attached the screenshot. you can change the content Type as Username in the storyboard or you can do programatically.
you may create an extension for UITextfield .
Thanks to Apple, I couldn't find any way with native methods when isSecureTextEntry setted to YES. Gal Shahar's way is the only solution for disabling password autofill accessory view option. But It's more easy to do with
Crazy staff happening in this topic. I've made without iOS proposing me a password, but given me an autofill for email only. Just if someone need it so. After different combinations and different type of textContentType I've made it as I wanted.
Email Text field with iOS autofill proposing be something.
Password text field with no iOS system proposing me a new password, but filling it with password if I choose it from email text field.
Password text field to have secure entry text.
And with this code it worked. Doesn't matter if you have email or username it will make you a proposition with what you need. So I've disabled accessory autofill view and left only autofill in toolbar of keyboard.
Ok, I can confirm that I have the following working on iOS 14 with no issues:
// once this property is declared, textField.keyboardType does
// not seem to have any effect in showing the autofill
textField.textContentType = UITextContentType.oneTimeCode
textField.keyboardType = UIKeyboardType.emailAddress
In my case the solution with setting textContentType = .oneTimeCode for password fields worked only partially. On the device with iOS 15 I did not saw a problem but on the simulator with iOS 15 it did not work properly especially when switching between the fields. Also it's worth saying that I had a button for showing/hiding passwords in fields which toggle the value in isSecureTextEntry. However after setting textContentType = .oneTimeCode on all fields on the screen (not only password fields but also other name and email fields which I had) then it started to work properly also on the simulator. So if the .oneTimeCode solution does not work for you then you might want to try setting it on all fields on your screen not only password fields.