How to implement AutoFill OTP verfication and Auto Proceed in iOS

Photo by Gunnar Sigurðarson on Unsplash

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

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.
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
Sample Project File’s ViewController.Swift Code to perfom AutoFill OTP
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.
  • 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

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.
  • Create a Notification to monitor textField and call KeyboardDidShow function
NotificationCenter.default.addObserver( self,selector:#selector(self.keyboardDidShow), name: UITextField.textDidChangeNotification, object: OTPTextField)
  • When OTP is filled Automatically ,notification will call keyboardDidShow function and we can navigate to next screen
@objc func keyboardDidShow(notifcation: NSNotification) {
if OTPTextField.text?.count == 4 {
buttonContinue.isEnabled = true
} else {
buttonContinue.isEnabled = false
@IBAction func buttonLogin(_ sender: UIButton) {
let mainStoryboardId : UIStoryboard = UIStoryboard(name: “Main”, bundle: nil)
let StartVC = mainStoryboardId.instantiateViewController(withIdentifier: “StartViewController”) as! StartViewController
self.navigationController?.pushViewController(StartVC, animated: true)
  • 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.
AutoProceed after Auto Fill OTP - Demo

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

After doing everything I mentioned above AutoFill OTP and Auto Proceed will work .

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

I hope you found this article helpful. If you did, please don’t hesitate to clap or share this post on Twitter or your social media of choice, every share helps me to write more. If you have any queries, feel free to comment below and I’ll see what I can do.Thanks.

Let’s connect!

You can find me on Twitter | LinkedIn | GitHub

iOS and Xamarin Developer.I love to write articles regarding Techs,iOS and Xamarin concepts. Follow me on GitHub:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store