How to implement Automatic OTP verification in iOS

How to implement Automatic OTP verification in iOS

·

3 min read

From iOS 12, Apple has allowed the support to read One Time Code(OTP — One Time Password) which you will get in the iPhone device.

iOS supports Password AutoFill on UITextField, UITextView, and any custom view that adopts the UITextInput protocol.

I have implemented a sample project to demonstrate how to AutoFill One Time Security in iOS and how to do Auto Proceed after OTP is Filled.

This features works on (Xcode 10 or above) and in (iOS 12 or above)

AutoFill OTP

1) StoryBoard Designing

Simple design in storyboard to Auto Read OTP

  • I have used UITextField and UIButton in storyboard.

  • UITextField is used to Auto Fill the One Time Code(OTP) and UIButton is to clear the textField and make it empty.

2) Make Changes to perform Auto Fill OTP in Storyboard and in Code

Changes needs to make in Storyboard to perform AutoFill OTP

To implement this in storyBoard, you need to change ,

  • UITextField of OTP’s textContentType to oneTimeCode. to read OTP from message.

  • UITextField of OTP’s keyboard Type to NumberPad. to open keyboard Type as numberPad

To implement this in code, you need to perform below in swift file,

`OTPTextField.textContentType = .oneTimeCode  
`OTPTextField.keyboardType = .numberPad

3) Sample Implementation of ViewController.swift File

Sample Project File’s ViewController.Swift Code to perfom AutoFill OTP

4) Check how AutoFill OTP Works in Project

When messsage arrive,Passcode will show automatically in Keyboard

  • When message arrive , keyboard will automatically show suggestion based on the newly arrived message and you can simply type to fill the textField which is shown in below image.

After Tapping suggestion in keyboard will fill the textField automatically

  • You can clear the textField by tapping clear button and after next message arrive, keyboard will automatically show next suggestion based on message.

5) Things you should do to make AutoFill Work all the time:

  • Use system Keyboard and default TextField with minimum customization

  • Customized textfield will also work but not all the time.

  • Message should contain passcode or code keyword.

Auto Proceed after AutoFill OTP

1) Designing

AutoFill OTP implementation — Storyboard Designing

  • Embed Navigation Controller to ViewController

  • Add Continue button to ViewController and make sure is not enabled until OTP is Filled

  • Designing new Viewcontroller to navigate after OTP Auto Fill.

2) Implementation of AutoProceed after AutoFill OTP

ViewController.swift

  • Create a Notification to monitor textField and call KeyboardDidShow function

  • When OTP is filled Automatically ,notification will call keyboardDidShow function and we can navigate to next screen

  • When textField is filled automatically, it will call keyboardDidShow(notifcation: NSNotification) function and keyboardDidShow(notifcation: NSNotification) function will call buttonLogin(_ sender: UIButton) and this function itself push to next view controller by using storyboard Id.

I have used simulator here but it will work perfectly in real device after OTP auto Fill.

This project is updated for Xcode 10 and Swift 5.0 . AutoFill OTP feature is only works for version iOS 12 and above.