Automatic OTP Verification In iOS Using Xamarin.Forms

Introduction

 
Passwords and security codes (OTP) are a modern necessity required for security and privacy but initially, iOS did not support the auto-read feature. iOS 12 eases the tedious aspects of account setup and sign-in by automatically suggesting and using strong, unique passwords and by bringing a one-time password to the QuickTypebar so users can fill them in one tap. Here, we’ll learn how to implement autofill OTP in our app using Xamarin.Forms.
 
Automatic OTP verification in iOS using Xamarin Forms
 
Message Format Rules
  • Security code only works with System keyboard. So, avoid using a custom keyboard.
  • Make sure the OTP message phrase has “code” or “passcode” and the message is a copied.

Create New Xamarin.Forms Application

 
In order to implement the Autofill OTP message, let’s start creating a new Xamarin.Forms project using Visual Studio 2019 or VS for Mac. When accessing Visual Studio 2019 Mac for the first time, you will come across a new interface for opening and creating the projects.
 
Open Visual Studio Mac >>Create New Project or select "Open Recent Application".
 
Automatic OTP verification in iOS using Xamarin Forms
 
The available templates will appear on a Mac like below. Select Xamarin.Forms application with different mobile platforms.
 
Automatic OTP verification in iOS using Xamarin Forms
 

Custom Control

 
The "OneTimeCode" type is available only specific to the iOS device, not in Android. So, create a Xamarin.Forms custom entry control in the .NET Standard library project, enabling developers to override the default native rendering with their own platform-specific customization.
  1. using System;  
  2. using Xamarin.Forms;  
  3.   
  4. namespace ReadOTPIOS  
  5. {  
  6.     public class AutoFillControl : Entry  
  7.     {  
  8.         public AutoFillControl()  
  9.         {  
  10.         }  
  11.     }  
  12. }  

UI Design

 
The AutoFillControl control can be referenced in XAML in the .NET Standard library project by declaring a namespace for its location and using the namespace prefix on the control element. The following code example shows how the AutoFillControl control can be consumed by a XAML page.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"   
  4.              xmlns:local="clr-namespace:ReadOTPIOS" x:Class="ReadOTPIOS.MainPage"  
  5.              >  
  6.   
  7.     <StackLayout Orientation="Horizontal">  
  8.         <Label  
  9.             FontSize="Large"  
  10.             HorizontalOptions="Center"  
  11.             Text="Welcome to iOS 12 Read and Autofill OTP from SMS using Xamarin Forms" />  
  12.   
  13.         <local:AutoFillControl  
  14.             FontSize="Large"  
  15.             HorizontalOptions="Center"  
  16.             Keyboard="Numeric"  
  17.             Placeholder="XXXXXX" />  
  18.     </StackLayout>  
  19. </ContentPage>  

IOS Renderer

 
The following code example shows the custom renderer for the iOS platform. The call to the base class's OnElementChanged method instantiates an iOS UITextField control, with a reference to the control being assigned to the renderer's Control property.
 
To enable password autofill on a UITextField, we need to set the UItextContentTypeproperty as a OneTimeCode.
  1. using ReadOTPIOS;  
  2. using ReadOTPIOS.iOS;  
  3. using UIKit;  
  4. [assembly: ExportRenderer(typeof(AutoFillControl), typeof(AutoFillRenderer))]  
  5. namespace ReadOTPIOS.iOS  
  6. {  
  7.     public class AutoFillRenderer: EntryRenderer  
  8.     {  
  9.         protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)  
  10.         {  
  11.             base.OnElementChanged(e);  
  12.   
  13.             if (e.NewElement != null)  
  14.             {  
  15.                 Control.TextContentType = UITextContentType.OneTimeCode;  
  16.             }  
  17.         }  
  18.     }  
  19. }  
The application source code is available on GitHub . The iOS simulator won’t allow you to send messages, so you need to try and deploy the application to an actual iOS device and send a message to the device for testing.
 
Automatic OTP verification in iOS using Xamarin Forms
 

Summary

 
This article has demonstrated how to create a Read and Autofill OTP from SMS using Xamarin.Forms Entry control. I hope this article will help you. Please leave your feedback/query using the comments box, and if you like this article, please share it with your friends.


Similar Articles