Automatic OTP verification in iOS?

IosIphoneSmsOne Time-PasswordIos12

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

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 - Ios

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 - Ios

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 - Ios

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.

https://i.stack.imgur.com/gJ6AH.gif" width="350" />

Solution 5 - Ios

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).

Solution 6 - Ios

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

Solution 7 - Ios

I've got sollution from answer of [santosh kumar][1] and [Ted][2]

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

  [1]: https://stackoverflow.com/a/51170039/11077258
  [2]: https://stackoverflow.com/a/47195674/11077258

Solution 8 - Ios

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

Solution 9 - Ios

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

1. set your input filed 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 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 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

There is 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

Solution 10 - Ios

You can get OTP from your message.

otptextField.textContentType = .oneTimeCode

Can please get the project from his link.

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

Solution 11 - Ios

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")
        }

Solution 12 - Ios

No.

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

Solution 13 - Ios

if(server.google==server.connected)}{
   return server;
}

when connected make a lambda ( e-> ""); !!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRinshad KammathView Question on Stackoverflow
Solution 1 - IosiVarunView Answer on Stackoverflow
Solution 2 - IosiVentisView Answer on Stackoverflow
Solution 3 - IosRamkrishna SharmaView Answer on Stackoverflow
Solution 4 - IosTedView Answer on Stackoverflow
Solution 5 - IosEl0dinView Answer on Stackoverflow
Solution 6 - IosAntDCView Answer on Stackoverflow
Solution 7 - IossteveSarsawaView Answer on Stackoverflow
Solution 8 - Iosknig_TView Answer on Stackoverflow
Solution 9 - IosFa.ShapouriView Answer on Stackoverflow
Solution 10 - IosKarthick CView Answer on Stackoverflow
Solution 11 - IosSanjay MaliView Answer on Stackoverflow
Solution 12 - IosB. BorseView Answer on Stackoverflow
Solution 13 - IosMikaView Answer on Stackoverflow