57

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.

Sahil Kapoor
  • 11,183
  • 13
  • 64
  • 87
Rinshad Kammath
  • 571
  • 1
  • 4
  • 3

12 Answers12

47

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

iVarun
  • 6,496
  • 2
  • 26
  • 34
  • @Waqas is it working on other devices expect iPhone 7? – iVarun Nov 29 '18 at 07:23
  • 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, – Waqas Nov 29 '18 at 07:25
  • @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. – iVarun Nov 29 '18 at 07:27
  • 1
    you are right that point is understood and well clear. I was asking about Autofill. In apple WWDC Session https://developer.apple.com/videos/play/wwdc2018/204/ It also talks about autofill passwords – Waqas Nov 29 '18 at 07:45
  • @iVarun It should, if you have iOS 12. I tried in iPhone SE, works super fine. – byJeevan Dec 08 '18 at 06:40
  • @byJeevan Yeah... It’s working fine. Waqas was looking for different answer – iVarun Dec 08 '18 at 06:45
  • **Also check SMS content to make sure that OTP number is Detected as OTP, NOT as phone Number** – Lal Krishna Sep 23 '19 at 06:36
  • is there specific format to identify one time code from message content? – Dhaval H. Nena Oct 10 '19 at 08:42
  • sorry i wanna ask is this .onetimecode can also works with whatsapp otp? or its just work with SMS otp? – afi permana Jul 12 '21 at 09:09
16

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.

iVentis
  • 993
  • 6
  • 19
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.

Ramkrishna Sharma
  • 6,961
  • 3
  • 42
  • 51
11

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.

bora.oren
  • 3,439
  • 3
  • 33
  • 31
Ted
  • 22,696
  • 11
  • 95
  • 109
  • 1
    is there any specific way to make this compatible with website otp's as well or does it just work by default? – Amien Jul 16 '18 at 16:16
  • can you give me guideline for this in ios 11 ? – Himanshu Moradiya Mar 07 '19 at 04:32
  • It is not automatic right now as in android, but this is the best you can do. codeTextField.textContentType =.oneTimeCode – Ted Mar 07 '19 at 08:48
  • 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? – Umang Galaiya Mar 26 '19 at 20:31
  • This one is done by apple pay probably using private APIs. Not yet available for public as far as I know – Ted Mar 26 '19 at 21:22
  • @Ted I've seen a third-party app using this as well. – Umang Galaiya Mar 27 '19 at 10:16
  • @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 – Rajan Maheshwari Jun 15 '19 at 20:40
  • 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. – Umang Galaiya Jun 28 '19 at 08:50
10

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

AntDC
  • 1,807
  • 14
  • 23
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).

El0din
  • 3,208
  • 3
  • 20
  • 31
7

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

Fa.Shapouri
  • 988
  • 2
  • 12
  • 30
6

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

Arshad Shaik
  • 1,095
  • 12
  • 18
steveSarsawa
  • 1,559
  • 2
  • 14
  • 31
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

knig_T
  • 1,916
  • 16
  • 16
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

Karthick C
  • 1,519
  • 17
  • 16
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")
        }
Sanjay Mali
  • 506
  • 6
  • 13
-10

No.

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

Nick is tired
  • 6,860
  • 20
  • 39
  • 51
  • 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. https://developer.apple.com/videos/play/wwdc2018/204/ – Mohammad Reza Koohkan Feb 21 '22 at 10:27