-1

I have a weird problem i am facing with. I know about OTP autofill with

textField.textContentType = .oneTimeCode .

But i have a different problem, i want this autofill to work but the text field should be disabled for any manual typing.

Basically , user gets message with OTP on his/her phone and OTP gets autofilled by using .oneTimeCode. But in any case if the app can't read or find OTP through this method(from SMS) , the user should NOT be allowed to manually enter the OTP.

Is there anyway to attain this scenario ?

Zyfe3r
  • 653
  • 6
  • 24
  • 1
    You need to disable user interaction of textfield like this:-textField.isUserInteractionEnabled = false – iOS Developer Mar 02 '20 at 06:00
  • But if i disable ``isUserInteractionEnabled`` , how will the OTP autofill? Doesn't the OTP appear on the keyboard ? – Zyfe3r Mar 02 '20 at 06:12

2 Answers2

1

You can set a transparent view on textField & make its userInteraction disable. & for showing keyboard automatically you can use textField.becomeFirstResponder()

You need to disable userInteraction of view that is on your TextField:

transparentView.isUserInteractionEnabled = false

And for display keyboard automatically:

Yoy can write this line in viewWillAppear method when your otpScreen appear.

textField.becomeFirstResponder()
urvashi koladiya
  • 168
  • 1
  • 11
  • But if i disable ``isUserInteractionEnabled`` , how will the OTP autofill? Doesn't the OTP appear on the keyboard ? The keyboard needs to appear to have the OTP appear on the keyboard. – Zyfe3r Mar 02 '20 at 06:13
  • Yes you are right, then you can set a transparent view on textField & make its userInteraction disable. & for showing keyboard automatically you can use textField.becomeFirstResponder(). I hope this will wok. – urvashi koladiya Mar 02 '20 at 06:34
  • Yes this will work BUT, where would i use ``textField.becomeFirstResponder()`` , i can't just call it out of nowhere?! Like is there a way to find when OTP has been received ? – Zyfe3r Mar 02 '20 at 06:35
  • I think we can not detect that when OTP has been received, but instead of that you can use ```textField.becomeFirstResponder()``` when your otpScreen appear. – urvashi koladiya Mar 02 '20 at 06:45
  • i'll check if this is possible. – urvashi koladiya Mar 02 '20 at 06:46
  • https://stackoverflow.com/questions/21793885/detect-incoming-sms-with-specific-text-in-ios – urvashi koladiya Mar 02 '20 at 07:15
  • The thing is , user registers , OTP is sent from backend as part of registration. And it automatically goes to next screen , the OTP entering screen. Here there will be a field to enter the OTP. The OTP needs to be entered automatically but the user should NOT be allowed to manually enter. But the issue is that, the autocompletion OTP text comes on top of the keyboard, therefore keyboard NEEDS to APPEAR ! Also i am NOT trying to check if sms has been received , i just wana know if the OTP has been copied by the keyboard OR maybe allow user to click on OTP text on keyboard. – Zyfe3r Mar 02 '20 at 07:16
  • When your OTP screen appear. write this code (textField.becomeFirstResponder()) in viewWillAppear method. so your keyboard will stay appeared & when OTP will receive it will comes on top of the keyboard. or If you are using only one ViewController for your registerScreen and OTPScreen then you are hide one screen at a time so in this case you can check ```(if otpScreen.isHidden == false { textField.becomeFirstResponder() })```. this will work, i have set this in my project. – urvashi koladiya Mar 02 '20 at 07:26
  • Lemme try it and get back to you. – Zyfe3r Mar 02 '20 at 09:16
  • @Zyfe3r, did this solution worked for you? – Coder Apr 16 '23 at 10:20
  • @Coder Nope. Doesn't work. – Zyfe3r Apr 21 '23 at 09:32
0

At first glance, my suggestion will be adding clear view to block the text field touch, but this will prevent filling the OTP

Disabling user interaction will be preventing filling the OTP too.

Maybe you can use text field delegate on text change, check the new characters. When the characters are less than your OTP length then just ignore the new characters.

textField:shouldChangeCharactersInRange:replacementString:

antonio yaphiar
  • 450
  • 3
  • 9