OTP Auto-Fill Entry for iOS with .NET MAUI

I needed OT Auto-Fill for my .NET MAUI App, and since it involved an unexpected struggle to get it working I thought I'd share my findings.

Our app sends out a verification code via SMS and I want to capture this code and auto-fill it in the verification code field.

First of all I found Gerald's article for doing this in Xamarin - https://blog.verslu.is/xamarin-forms-xamarin/otp-auto-fill-entry-for-ios-with-xamarin-forms/

It was also rather easy to find articles of people extending EntryRenderer, which is an obsolete method.

The approach that I finally got working is to instead extend the EntryHandler, as such:


Now you need to do some wiring up in MauiProgram.cs. First of all the right usings:


Followed by adding the CustomEntryHandler in ConfigureMauiHandlers, as such:


Now - one would think that this was enough, but not quite! The problem was not quite expected. Our verification SMS contained just the verification code - 6 digits. In order for OTP to work, the message needs to contain something more. I haven't found solid documentation on the exact format needed, but this SO post got me on the right track: https://stackoverflow.com/a/61862437/11967220

The message format I went with was $"Your verification code is {verificationCode}" and that made it finally work!

Kommentarer