Password autofill is not working in swift

Viewed 276

there are 6 text fields to accepts otp(one time password).But when i am getting otp and trying to Password AutoFill on UITextField, only last 4 filleds are taking otp. But not all the fields. I have attached the screen shot

Here i have made all the textContentType to .oneTimeCode How to resolve the issue please help.

https://i.stack.imgur.com/kkIuQ.jpg

2 Answers

Try naming your text fields serially txt1 txt2 txt3 txt4 tt5 txt6

override func viewDidLoad() {
 super.viewDidLoad()
 txt1.becomeFirstResponder()
}

 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if ((textField.text?.count)! < 1 ) && (string.count > 0) {
            if textField == txt1 {
                txt2.becomeFirstResponder()

            }

            if textField == txt2 {
                txt3.becomeFirstResponder()

            }

            if textField == txt3 {
                txt4.becomeFirstResponder()
            }

            if textField == txt4 {
                txt5.becomeFirstResponder()
            }
            if textField == txt5 {
                txt6.becomeFirstResponder()
            }
            if textField == txt6{
                txt6.becomeFirstResponder()
            }

            textField.text = string
            return false
        } else if ((textField.text?.count)! >= 1) && (string.count == 0) {
            if textField == txt2 {
                txt1.becomeFirstResponder()
            }
            if textField == txt3 {
                txt2.becomeFirstResponder()

            }
            if textField == txt4 {
                txt3.becomeFirstResponder()
            }

           if textField == txt5 {
                txt4.becomeFirstResponder()
            }
            if textField == txt6 {
                txt5.becomeFirstResponder()
            }
            if textField == txt1{
               txt6.resignFirstResponder()
            }

            textField.text = ""
            return false
        } else if (textField.text?.count)! >= 1 {
            textField.text = string
            return false
        }

        return true
    }

Happy Coding

try this for autofill textfield

txtOTP.textContentType = .oneTimeCode
Related