Automatic OTP verification in iOS?
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.
When you get OTP it will look something like this:
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
instoryboard/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.
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.
Admin
Updated on June 08, 2021Comments
-
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 almost 6 yearsis there any specific way to make this compatible with website otp's as well or does it just work by default?
-
iVarun over 5 years@Waqas is it working on other devices expect iPhone 7?
-
Waqas over 5 yearsYes, 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 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 over 5 yearsyou 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 over 5 years@iVarun It should, if you have iOS 12. I tried in iPhone SE, works super fine.
-
iVarun over 5 years@byJeevan Yeah... It’s working fine. Waqas was looking for different answer 😅
-
Himanshu Moradiya about 5 yearscan you give me guideline for this in ios 11 ?
-
Ted about 5 yearsIt is not automatic right now as in android, but this is the best you can do. codeTextField.textContentType =.oneTimeCode
-
Umang Galaiya about 5 yearsThis 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 about 5 yearsThis one is done by apple pay probably using private APIs. Not yet available for public as far as I know
-
Umang Galaiya about 5 years@Ted I've seen a third-party app using this as well.
-
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 almost 5 yearsCan'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 over 4 yearsAlso check SMS content to make sure that OTP number is Detected as OTP, NOT as phone Number
-
Dhaval H. Nena over 4 yearsis there specific format to identify one time code from message content?
-
JOM about 4 yearsSigh. 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 about 4 yearsI know your pain...Hence my post.
-
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 almost 4 yearsI am facing this problem stackoverflow.com/questions/61495065/…
-
Gaurav Borole almost 4 yearsI am facing this problem stackoverflow.com/questions/61495065/…
-
steveSarsawa almost 4 years@GauravBorole if you facing that issue, then please set
textContentType
to.oneTimeCode
only for OTP textField. -
Nirav Kotecha over 3 yearswhat if i not clicked on "From Messages 180605" ? i want to fill textfiled without user click.
-
Ramkrishna Sharma over 3 years@iNiravKotecha I beleive it is not possible as how you will get the OTP data without click.
-
Nirav Kotecha over 3 years@RamkrishnaSharma i want like android. sms come and it will fill in textfield.
-
steveSarsawa about 3 years@DilipTiwari what issue are u facing?
-
steveSarsawa about 3 years@DilipTiwari "Message should contain code or passcode KeyWord"
-
Dilip Tiwari about 3 yearsI get otP message text as "3125" only and message name comes from AD-DSHOTP
-
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 about 3 yearsHii @steveSarsawa by changing msg content as you suggested it resolved by issue thanks a lot i forgot to thank you.
-
steveSarsawa about 3 years@DilipTiwari You can thank me by upvoting my answer ;)
-
afi permana almost 3 yearssorry i wanna ask is this .onetimecode can also works with whatsapp otp? or its just work with SMS otp?
-
Mohammad Reza Koohkan about 2 yearsSince 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