Automatic OTP verification in iOS?

81,761

Solution 1

In iOS 12 Apple has introduced feature called Security Code AutoFill.

To use this in your app all you need to do is set UITextField's input view’s textContentType property oneTimeCode.

otpTextField.textContentType = .oneTimeCode

NOTE: Security Code AutoFill will only works with System Keyboard it will not work with custom keyboard.

WWDC video

When you get OTP it will look something like this:

enter image description here

Solution 2

It is also important that the text message you receive contains something with "code" like

"your passcode is:123456"

or

"12345 is your code to log in"

something along that line.

NOT!

Your App: 12345

you can verify if the code in your text message will work with the .oneTimeCode type by tapping the underlined code in your message. If a dialog pops up that says "copy code", you are good to go. Otherwise you might need to change the text of your message.

Solution 3

UPDATE

From iOS 12 Apple will supports Password AutoFill on UITextField, UITextView, and any custom view that adopts the UITextInput protocol. System keyboard set the textContentType on it to .oneTimeCode

1) Using Code

singleFactorCodeTextField.textContentType = .oneTimeCode

2) Using Storyboard/XIB

Select UITextField/UITextView in storyboard/XIB click Click on Attribute inspector. Go to text input trait, click to Content type and select one time code and done.

The operating system will detect verification codes from Messages automatically with this UITextContentType set.

Warning

If you use a custom input view for a security code input text field, iOS cannot display the necessary AutoFill UI.

WWDC 2018 iPhoneX Device

For more information, you can check it on the Apple developer oneTimeCode

And also review WWDC 2018 Session 204 - Automatic Strong Passwords and Security Code AutoFill and jump to 24:28 for automatic pre-fill the OTP.

Solution 4

Currently for iOS 12 and above, you may use Security Code Autofill

oneTimeCodeTextField.textContentType =.oneTimeCode

However ApplePay is doing automatic verification since iOS 11 but that is not yet available to developers.

Solution 5

Also...on the phone "Autofill Passwords" needs to be turned on.

Share:
81,761
Admin
Author by

Admin

Updated on June 08, 2021

Comments

  • Admin
    Admin almost 3 years

    Is there any way to access data from iPhone inbox(SMS) to ios application to do automatic OTP verification like the one in Android? I shall be grateful for your help.

  • Amien
    Amien almost 6 years
    is there any specific way to make this compatible with website otp's as well or does it just work by default?
  • iVarun
    iVarun over 5 years
    @Waqas is it working on other devices expect iPhone 7?
  • Waqas
    Waqas over 5 years
    Yes, iphone 7 isn't asking me to save password or fill it when signing-in. I guess it needs some account saved in keychain before using it,
  • iVarun
    iVarun over 5 years
    @Waqas It will not ask you to save the password. When you get OTP you will see a toolbar above the keyboard with OPT code.
  • Waqas
    Waqas over 5 years
    you are right that point is understood and well clear. I was asking about Autofill. In apple WWDC Session developer.apple.com/videos/play/wwdc2018/204 It also talks about autofill passwords
  • byJeevan
    byJeevan over 5 years
    @iVarun It should, if you have iOS 12. I tried in iPhone SE, works super fine.
  • iVarun
    iVarun over 5 years
    @byJeevan Yeah... It’s working fine. Waqas was looking for different answer 😅
  • Himanshu Moradiya
    Himanshu Moradiya about 5 years
    can you give me guideline for this in ios 11 ?
  • Ted
    Ted about 5 years
    It is not automatic right now as in android, but this is the best you can do. codeTextField.textContentType =.oneTimeCode
  • Umang Galaiya
    Umang Galaiya about 5 years
    This seems to automatically input the OTP in the input field as well, while the other examples just show a suggestion on top of the keyboard. Is there any difference in the implementation between the two?
  • Ted
    Ted about 5 years
    This one is done by apple pay probably using private APIs. Not yet available for public as far as I know
  • Umang Galaiya
    Umang Galaiya about 5 years
    @Ted I've seen a third-party app using this as well.
  • Rajan Maheshwari
    Rajan Maheshwari almost 5 years
    @UmangGalaiya Can you specify that third party? Coz AFAIK, this only works when we tap on suggestion on top of the keyboard. I too haven't seen any third party automatically populating the textfield as soon as we get a message
  • Umang Galaiya
    Umang Galaiya almost 5 years
    Can't name the third party, but they were definitely auto-reading the OTP and not relying on the suggestion that shows up on top of the keyboard. They've rolled back the feature since about two weeks after my first comment on this answer.
  • Lal Krishna
    Lal Krishna over 4 years
    Also check SMS content to make sure that OTP number is Detected as OTP, NOT as phone Number
  • Dhaval H. Nena
    Dhaval H. Nena over 4 years
    is there specific format to identify one time code from message content?
  • JOM
    JOM about 4 years
    Sigh. This is the ONLY place I've seen to mention this. And I've been debugging and wondering why doesn't the code work...
  • AntDC
    AntDC about 4 years
    I know your pain...Hence my post.
  • Arshad Shaik
    Arshad Shaik almost 4 years
    @AntDC i am using iOS 13.4.1 keyboard is not showing auto fill suggestion, can you please help
  • Gaurav Borole
    Gaurav Borole almost 4 years
    I am facing this problem stackoverflow.com/questions/61495065/…
  • Gaurav Borole
    Gaurav Borole almost 4 years
    I am facing this problem stackoverflow.com/questions/61495065/…
  • steveSarsawa
    steveSarsawa almost 4 years
    @GauravBorole if you facing that issue, then please set textContentType to .oneTimeCode only for OTP textField.
  • Nirav Kotecha
    Nirav Kotecha over 3 years
    what if i not clicked on "From Messages 180605" ? i want to fill textfiled without user click.
  • Ramkrishna Sharma
    Ramkrishna Sharma over 3 years
    @iNiravKotecha I beleive it is not possible as how you will get the OTP data without click.
  • Nirav Kotecha
    Nirav Kotecha over 3 years
    @RamkrishnaSharma i want like android. sms come and it will fill in textfield.
  • steveSarsawa
    steveSarsawa about 3 years
    @DilipTiwari what issue are u facing?
  • steveSarsawa
    steveSarsawa about 3 years
    @DilipTiwari "Message should contain code or passcode KeyWord"
  • Dilip Tiwari
    Dilip Tiwari about 3 years
    I get otP message text as "3125" only and message name comes from AD-DSHOTP
  • steveSarsawa
    steveSarsawa about 3 years
    @DilipTiwari sorry but your msg must contain the "Code/CODE" or "Passcode/PASSCODE" keyword. Unless it won't be work.
  • Dilip Tiwari
    Dilip Tiwari about 3 years
    Hii @steveSarsawa by changing msg content as you suggested it resolved by issue thanks a lot i forgot to thank you.
  • steveSarsawa
    steveSarsawa about 3 years
    @DilipTiwari You can thank me by upvoting my answer ;)
  • afi permana
    afi permana almost 3 years
    sorry i wanna ask is this .onetimecode can also works with whatsapp otp? or its just work with SMS otp?
  • Mohammad Reza Koohkan
    Mohammad Reza Koohkan about 2 years
    Since you answered this question in 2017, there was no solution to the questioner issue but back in 2018 apple introduced Security Code AutoFill which helps reading code from SMS box. developer.apple.com/videos/play/wwdc2018/204