I am trying to implement the OTP SMS autofill feature in my Xamarin iOS application. Specifically, I want the OTP code to appear directly on the keyboard suggestion bar when an SMS containing the code is received. However, I am having trouble getting this feature to work despite following the available documentation and examples. The OTP code does not appear on the keyboard as expected.
I have tried to implement the OTP SMS autofill feature in my Xamarin iOS application by following various tutorials and the official documentation. Specifically, I used the following code to enable the OTP autofill:
{
public class GdOtpEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (UIDevice.CurrentDevice.CheckSystemVersion(12, 0) && Control != null)
{
Control.Layer.CornerRadius = 10;
Control.TextContentType = UITextContentType.OneTimeCode;
}
}
}
}
public class GdOtpEntry : Entry
{
public GdOtpEntry()
{
IsVisible = true;
BackgroundColor = Xamarin.Forms.Color.White;
FontSize = 10;
WidthRequest = 40;
HorizontalOptions = LayoutOptions.Start;
TextColor = Color.Black;
}
}
private GdOtpEntry _otpEntry;
public AuthenticationBottomPage(List<object> val = null)
{
InitializeComponent();
if (Device.RuntimePlatform == Device.iOS)
{
_otpEntry = new GdOtpEntry();
UpdateiOSEntries(_otpEntry);
}
else if (Device.RuntimePlatform == Device.Android)
{
MessagingCenter.Subscribe<App, string>(this, "UpdateOtp", (sender, receivedOtp) =>
{
_otp = receivedOtp;
UpdateEntries(_otp);
});
}
Init(val);
}
private async void UpdateiOSEntries(GdOtpEntry otpEntry)
{
await GdApp.Instance.UiManager.ShowLoadingAsync();
await Task.Delay(4000);
if (!string.IsNullOrEmpty(otpEntry.Text) && otpEntry.Text.Length >= 4)
{
Device.BeginInvokeOnMainThread(async () => {
_entryFirstRect.Text = otpEntry.Text[0].ToString();
_entrySecRect.Text = otpEntry.Text[1].ToString();
_entryThirdtRect.Text = otpEntry.Text[2].ToString();
_entryFourtRect.Text = otpEntry.Text[3].ToString();
await GdApp.Instance.UiManager.HideLoadingAsync();
});
}
else
{
await GdApp.Instance.UiManager.HideLoadingAsync();
}
} '''
xamarincodejunior is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.