Automatic OTP verification in iOS?
Asked Answered
G

12

58

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.

Geer answered 22/9, 2016 at 5:49 Comment(1)
don't think so, you have to ask them to enter manuallyNeap
A
48

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

Acie answered 11/6, 2018 at 6:27 Comment(9)
@Waqas is it working on other devices expect iPhone 7?Acie
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,Erasmo
@Erasmo It will not ask you to save the password. When you get OTP you will see a toolbar above the keyboard with OPT code.Acie
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 passwordsErasmo
@Acie It should, if you have iOS 12. I tried in iPhone SE, works super fine.Tympanist
@Tympanist Yeah... It’s working fine. Waqas was looking for different answer 😅Acie
Also check SMS content to make sure that OTP number is Detected as OTP, NOT as phone NumberAquamanile
is there specific format to identify one time code from message content?Transduction
sorry i wanna ask is this .onetimecode can also works with whatsapp otp? or its just work with SMS otp?Puglia
S
17

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.

Schlesinger answered 29/7, 2019 at 14:22 Comment(0)
C
12

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.

Crosseyed answered 9/11, 2017 at 6:55 Comment(8)
is there any specific way to make this compatible with website otp's as well or does it just work by default?Pinole
can you give me guideline for this in ios 11 ?Gilmore
It is not automatic right now as in android, but this is the best you can do. codeTextField.textContentType =.oneTimeCodeCrosseyed
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?Indign
This one is done by apple pay probably using private APIs. Not yet available for public as far as I knowCrosseyed
@Crosseyed I've seen a third-party app using this as well.Indign
@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 messageRick
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.Indign
S
12

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.

Spadix answered 24/9, 2018 at 4:17 Comment(4)
I am facing this problem #61495565Shorten
what if i not clicked on "From Messages 180605" ? i want to fill textfiled without user click.Antoniaantonie
@iNiravKotecha I beleive it is not possible as how you will get the OTP data without click.Spadix
@RamkrishnaSharma i want like android. sms come and it will fill in textfield.Antoniaantonie
K
11

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

Keaton answered 21/10, 2019 at 15:41 Comment(3)
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...Linkboy
I know your pain...Hence my post.Keaton
@Keaton i am using iOS 13.4.1 keyboard is not showing auto fill suggestion, can you please helpTrawick
W
10

To get otp on your text field two things to be observed

1. set your input field as otp

Other answers let you know how manage your code to receive otp.

2. provide "code" phrase in your message

Your message must contains "code", "passcode", "password" in english and if you want to enable it in other language test with some word translated from "code" to your language

I have received message with bellow words:

Spanish: (Codingo)

Germany: (Kode)

Czech: (Kod)

Persian: (رمز)

Arabic: (رمز)

The otp number must be in english not your language number, your "Code" phrase must be separate with only one space with your numbers or with ":" without space.

Code 1111111

Code:111111

One other important thing you must be attention is about message preview. The numbers will be detected as otp if it has a gray line in below. if it shows in blue color or without line, Message does not notify you as otp. Somthing like image

enter image description here

Wacke answered 7/6, 2021 at 10:4 Comment(2)
Just out of curiosity, what is your source for this information?Twist
I discovered them through testing and observing the resultsWacke
L
8

In Xamarin iOS, for >=iOS 12:

First of all, the SMS need to have the keyword "code" or "passcode" into their message, and don't use spaces after the code. if you received the SMS and you have the button "Copy Code" then it will works

enter image description here

Then you need to place this:

_txtField = new UITextField()
{
   UserInteractionEnabled = true,
};
if (UIDevice.CurrentDevice.CheckSystemVersion(12, 0))
{
  _txtField.TextContentType = UITextContentType.OneTimeCode;          
}
_txtFieldDelegate = new UITextFieldDelegate();
_txtField.Delegate = _txtFieldDelegate;
_txtField.BecomeFirstResponder();

NOTE: Security Code AutoFill will only works with System Keyboard (not custom).

Legofmutton answered 13/11, 2018 at 9:36 Comment(0)
D
7

I've got sollution from answer of santosh kumar and Ted

var otpText = String()
  • in viewDidload()
     if #available(iOS 12.0, *) {
         txtFirst.textContentType = .oneTimeCode
         txtSecond.textContentType = .oneTimeCode
         txtThird.textContentType = .oneTimeCode
         txtForth.textContentType = .oneTimeCode
         txtFifth.textContentType = .oneTimeCode
     }

     txtFirst.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
     txtSecond.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
     txtThird.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
     txtForth.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
     txtFifth.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
     txtFirst.becomeFirstResponder() //by doing this it will open the keyboard on first text field automatically

  • Action for TextField
   //When changed value in textField
    @objc func textFieldDidChange(textField: UITextField){
        let text = textField.text
        if  text?.count == 1 {
            switch textField{

            case txtFirst:
                txtSecond.becomeFirstResponder()
            case txtSecond:
                txtThird.becomeFirstResponder()
            case txtThird:
                txtForth.becomeFirstResponder()
            case txtForth:
                txtFifth.becomeFirstResponder()
            case txtFifth:
                txtFifth.resignFirstResponder()
                self.dismissKeyboard()
            default:
                break
            }
        }
        if  text?.count == 0 {
            switch textField{
            case txtFirst:
                txtFirst.becomeFirstResponder()
            case txtSecond:
                txtFirst.becomeFirstResponder()
            case txtThird:
                txtSecond.becomeFirstResponder()
            case txtForth:
                txtThird.becomeFirstResponder()
            case txtFifth:
                txtForth.becomeFirstResponder()
            default:
                break
            }
        }
        else{

        }
    }
  • OTP String and Dismiss KeyBoard
 func dismissKeyboard(){

        self.otpText = "\(self.txtFirst.text ?? "")\(self.txtSecond.text ?? "")\(self.txtThird.text ?? "")\(self.txtForth.text ?? "")\(self.txtFifth.text ?? "")"

        print(self.otpText)
        self.view.endEditing(true)

    }

Most important thing: if you are using shouldChangeCharactersIn method, please comment it. Else this code will not work

Doersten answered 24/2, 2020 at 12:0 Comment(8)
I am facing this problem #61495565Shorten
@GauravBorole if you facing that issue, then please set textContentType to .oneTimeCode only for OTP textField.Doersten
@DilipTiwari what issue are u facing?Doersten
@DilipTiwari "Message should contain code or passcode KeyWord"Doersten
I get otP message text as "3125" only and message name comes from AD-DSHOTPNudi
@DilipTiwari sorry but your msg must contain the "Code/CODE" or "Passcode/PASSCODE" keyword. Unless it won't be work.Doersten
Hii @Doersten by changing msg content as you suggested it resolved by issue thanks a lot i forgot to thank you.Nudi
@DilipTiwari You can thank me by upvoting my answer ;)Doersten
D
4

you can easily set this in Storyboard

Click on Attribute inspector. Go to text input trait, click on Content type and select one time code

Desalinate answered 4/12, 2018 at 11:56 Comment(0)
S
2

You can get OTP from your message.

otptextField.textContentType = .oneTimeCode

Can please get the project from his link.

https://github.com/karthickkck315/Automatic-OTP

Sovran answered 27/12, 2018 at 10:32 Comment(0)
C
0

To support Autofill OTP Note : To read OTP from Messages, Message should contain code or passcode

if #available(iOS 12.0, *) {
            optTextField.textContentType = .oneTimeCode
        } else {
            // Fallback on earlier versions
            print("Fallback on earlier versions")
        }
Caithness answered 6/11, 2020 at 5:25 Comment(0)
B
-10

No.

Because this would be considered a privacy issue, you cannot access the users SMS inbox.

Brutalize answered 2/8, 2017 at 9:57 Comment(1)
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/204Disqualification

© 2022 - 2024 — McMap. All rights reserved.