Charitha Basnayake
Charitha Basnayake

Reputation: 303

Prevent Username Autofill in Incorrect UITextField with Biometric Authentication

I'm encountering an issue with the autofill behavior in one of my iOS applications. On an action confirmation screen, users can enter a reason in the first text field and their account password in the second text field for validation purpose. However, when users use biometric authentication (Touch ID/Face ID) to autofill the password, the username from the keychain is mistakenly being entered into the reason text field (over-rides the already filled reason).

Here’s the setup:

I've tried the following solutions without success:

  1. Setting textContentType to .none for the reasonTextField.
  2. Ensuring passwordTextField has textContentType set to .password.
  3. Using distinct accessibility identifiers for both text fields.

Here's a simplified version of my code:

import UIKit

class ActionConfirmationViewController: UIViewController {
    
    @IBOutlet weak var reasonTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        configureTextFields()
    }
    
    func configureTextFields() {
        reasonTextField.textContentType = .none
        reasonTextField.accessibilityIdentifier = "reasonTextField"
        
        passwordTextField.textContentType = .password
        passwordTextField.accessibilityIdentifier = "passwordTextField"
        passwordTextField.isSecureTextEntry = true
    }
}

Despite these measures, the issue persists. Has anyone experienced a similar problem or have any suggestions on how to prevent the username from being autofilled into the reason text field? Any help would be greatly appreciated!

Upvotes: 0

Views: 56

Answers (1)

alibarac
alibarac

Reputation: 11

Try this instead of it.

import UIKit

class ActionConfirmationViewController: UIViewController {
    
    @IBOutlet weak var reasonTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        configureTextFields()
    }
    
    func configureTextFields() {
        reasonTextField.textContentType = .oneTimeCode  // Use a more neutral type instead of .none
        reasonTextField.autocorrectionType = .no
        reasonTextField.spellCheckingType = .no
        reasonTextField.smartDashesType = .no
        reasonTextField.smartInsertDeleteType = .no
        reasonTextField.smartQuotesType = .no
        reasonTextField.accessibilityIdentifier = "reasonTextField"
        
        passwordTextField.textContentType = .password
        passwordTextField.autocorrectionType = .no
        passwordTextField.spellCheckingType = .no
        passwordTextField.isSecureTextEntry = true
        passwordTextField.accessibilityIdentifier = "passwordTextField"
    }
}

Using .oneTimeCode is better than .none.

Upvotes: 1

Related Questions