Skip to content

A .Net MAUI plugin for reading One-time Password (OTP) from SMS for Android and iOS

License

Notifications You must be signed in to change notification settings

Jake-Derrick/Plugin.Maui.OtpReader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nuget

Plugin.Maui.OtpReader

Plugin.Maui.OtpReader provides the ability to do automatically retrieve one-time passcodes from SMS messages in .NET MAUI.

AndroidOtpReader IosOtpReader

Install Plugin

NuGet

Available on NuGet.

Install with the dotnet CLI: dotnet add package Plugin.Maui.OtpReader, or through the NuGet Package Manager in Visual Studio.

Supported Platforms

Platform Minimum Version Supported
iOS 12+
macOS Not Supported
Android 5.0 (API 21)
Windows Not Supported

Usage

Unfortunately, the usage between Android and iOS differs, but both can be easily implemented within the same project.

iOS

The code will be automatically parsed and displayed above the keyboard, but it cannot be auto-filled. To display the code above the keyboard, set up an Entry box using the SetContentTypeAsOtp extension:

<Entry x:Name="OtpEntry" />
OtpEntry.SetContentTypeAsOtp();

Alternatively, in C#:

var OtpEntry = new Entry();
OtpEntry.SetContentTypeAsOtp();

Android

Although setting up on Android requires a bit more work, it offers greater flexibility in handling the code.

You can use OtpReader as a static class, e.g., OtpReader.Default;, or with dependency injection: builder.Services.AddSingleton<IOtpReader>(OtpReader.Default);.

Once you have access to OtpReader, you can subscribe to the OtpReceived event, which is triggered when the SMS message is received.

private readonly IOtpReader _otpReader;
public MainPage(IOtpReader otpReader)
{
  _otpReader = otpReader;
}

protected override void OnAppearing()
{
  _otpReader.OtpReceived += OnOtpReceivedFromSms
  base.OnAppearing();
}

private void OnOtpReceivedFromSms(string? otp)
{
  // Do something with the otp code here. For example, set an Entry's text to the otp code.
}

To trigger the OtpReceived event, you must first call the StartSmsListener method to begin listening for the SMS message. The listener will timeout after 5 minutes if no message is received, so start it right before you send the SMS message. You can optionally supply a regex to parse the code from the message. If no regex is supplied, the entire message contents will be returned.

private void StartListenerClicked(object sender, EventArgs e)
{
  var otpRegex = @"\d{6}" // Matches exactly 6 consecutive digits
  _otpReader.StartSmsListener(OtpRegex);
}

For the listener to read your SMS message, the message must contain a hash unique to your app. See Google's documentation for computing your apps hash. Alternatively, you can get your app's hash with the AppHashHelper class from the sample app.

About

A .Net MAUI plugin for reading One-time Password (OTP) from SMS for Android and iOS

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages