Swift – Read automatically pin-code from SMS / OneTimeCode

Very often you should insert a pin-code in a UITextField that some service in order to verify your identity sends you via SMS (generally for a 2FA, two-factor authentication).

Why do you need to write by yourself if you can read automatically and feel the UITextField directly?

Starting from iOS 12 you can do this, as Apple explained in WWDC.


Using interface builder_

Simply create an UITextField and choose from properties the content type = “One Time Code“.


Using code_

Using code, you can set the “textContentType"of your UITextField to .oneTimeCode


Run your app_

To simulate this, send yourself a message!

Note: your message must contain the wordcode” or “passcode“!

You receive on top of your keyboard the content of the pin-code.

Tapping on it, you fill automatically the selected UITextField.


 

Alberto Pasca

Software engineer @ Pirelli & C. S.p.A. with a strong passion for mobile  development, security, and connected things.