How to enable/disable OneTimeCode feature for specific text field?
Asked Answered
E

4

6

For OTP text field, I have set textContentType as oneTimeCode

if #available(iOS 12.0, *) {
     otpTextField.textContentType = .oneTimeCode
}    

Now when we receive OTP, iOS is showing OTP for all text fields for which keyboard type is set as numberPad e.g) Security Token, zipcode etc.

The app should only populate OTP for text field that has textContentType as oneTimeCode not for all text fields in the app.

How to enable/disable oneTimeCode for specific text field?

Ecchymosis answered 29/4, 2020 at 5:46 Comment(1)
Hi I am looking for answer for this. if you found solution please let me know.Destitution
C
8

I tried all sorts of content types to prevent OTP from being suggested on the keyboard, finally this one worked

if #available(iOS 12.0, *) {
    textField.textContentType = .username
}

In this case the UITextField is capturing a pin, and also has

keyboardType = .numberPad

Although that may not make a difference

Castro answered 23/10, 2020 at 8:59 Comment(2)
I have tried every solution I found, and you are right, setting .username is the only way for it to not show up in my textfield!Ivanna
@Castro +1. But If the textField's content type is the default(unspecified) and autoCorrection is also default and keyboard is also default, then also we get the one time code in that textField. For that, we have to make autoCorrection to No. But again if I require correction, then it wont work and one time code will show upHomogeneity
C
0

You need to change textContentType property in UITextField to one of the following type:

enter image description here

If you have this code enabled on all textFields, this means that input type of this textFields is one from supported list above. So make sure that you have correct textContentType in your textFields

Note:

  • If you use a custom input view for a security code input text field, iOS cannot display the necessary AutoFill UI ((c) Apple).
  • Make sure that message that is coming in SMS contains either "code" either "passcode" and message can be copied.
  • available from iOS 12

Reference to Official doc and this one

Cosh answered 29/4, 2020 at 6:4 Comment(1)
Please add multiple text fields on the same screen. e.g) Security Token, ZipCode. Set OneTImeCode to Security token only. iOS Populate code for zip code text field alsoEcchymosis
H
0

The solution for me was to disable autocorrection for the textfield:

textField.autocorrectionType = .no

If you require autocorrection and you don't want OTP suggestion, you have to explicitly define an option for textContentType property. (You can find the options in this page: https://developer.apple.com/documentation/uikit/uitextcontenttype). However, if none of the options fits your field semantics it's preferable to set unspecified to avoid wrong suggestions.

Hydantoin answered 12/7, 2021 at 9:4 Comment(1)
Didn't work for me, still suggesting sms OTP.Yarndyed
R
-2

Your approach with content-type is correct , but there are few things to consider to make it work , Such as

  • Security code is only work with System keyboard. So avoid using custom keyboard.
  • Make sure otp message phrase has “code” or “passcode” and message is copyable.

Also , I implemented this by following steps given in below link

Reference Link : https://medium.com/@shankarmadeshvaran/how-to-implement-automatic-otp-verification-in-ios-7813c6116a1d

Hope It Helps

Ramachandra answered 29/4, 2020 at 6:5 Comment(3)
Yes we are using the system keyboard only and otp message phrase has “code” or “passcode” and the message is capable. Still, we are facing issues...Ecchymosis
Please add multiple textFields on the same screen. e.g) Security Token, ZipCode. Set OneTImeCode to Security token only. iOS Populate code for zipcode text field alsoEcchymosis
@GauravBorole I will try with multiple textFields and then get back to you...Ramachandra

© 2022 - 2024 — McMap. All rights reserved.