'How can i disable input textfield from user with only One-Time-Code auto fill support

I want to disable manual text entering from one-time-code textField while the user only can tap SMS OTP Code from Keyboard Quicktype Bar.

Another question i got from seeing whatsapp is that their input shows the Keyboard Quicktype bar automatically while mine is not unless i call becomeFirstResponder

How can i achieve this?

Thanks.



Solution 1:[1]

You can try this, maybe it works for your use case

Remove textField.isEnabled = false if you added it before

Add textField.delegate = self so we can manage what happens when user adds input

Add textField.becomeFirstResponder() to make the keyboard appear

Then implement this UITextFieldDelegate callback

extension YourViewOrViewController: UITextFieldDelegate {
    
    func textField(_ textField: UITextField,
                   shouldChangeCharactersIn range: NSRange,
                   replacementString string: String) -> Bool {
        
        // Only allow multiple characters to be set like the OTP
        // Or define your own logic when you want text to be
        // accepted into the text field
        return string.count != 1
    }
}

Check if this gives you the desired result

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shawn Frank