Plugin.Maui.OtpReader
1.1.0
dotnet add package Plugin.Maui.OtpReader --version 1.1.0
NuGet\Install-Package Plugin.Maui.OtpReader -Version 1.1.0
<PackageReference Include="Plugin.Maui.OtpReader" Version="1.1.0" />
<PackageVersion Include="Plugin.Maui.OtpReader" Version="1.1.0" />
<PackageReference Include="Plugin.Maui.OtpReader" />
paket add Plugin.Maui.OtpReader --version 1.1.0
#r "nuget: Plugin.Maui.OtpReader, 1.1.0"
#:package Plugin.Maui.OtpReader@1.1.0
#addin nuget:?package=Plugin.Maui.OtpReader&version=1.1.0
#tool nuget:?package=Plugin.Maui.OtpReader&version=1.1.0
Plugin.Maui.OtpReader
Plugin.Maui.OtpReader provides the ability to do automatically retrieve one-time passcodes from SMS messages in .NET MAUI.
Install Plugin
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.</p>
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.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-android35.0 is compatible. net9.0-browser was computed. net9.0-ios was computed. net9.0-ios18.0 is compatible. net9.0-maccatalyst was computed. net9.0-maccatalyst18.0 is compatible. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-android36.0 is compatible. net10.0-browser was computed. net10.0-ios was computed. net10.0-ios26.0 is compatible. net10.0-maccatalyst was computed. net10.0-maccatalyst26.0 is compatible. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net10.0-windows10.0.19041 is compatible. |
-
net10.0
- Microsoft.Maui.Controls (>= 10.0.1)
-
net10.0-android36.0
- Microsoft.Maui.Controls (>= 10.0.1)
- Xamarin.AndroidX.Fragment.Ktx (>= 1.8.9)
- Xamarin.AndroidX.Lifecycle.LiveData (>= 2.9.2.1)
- Xamarin.AndroidX.Lifecycle.LiveData.Core (>= 2.9.2.1)
- Xamarin.AndroidX.Lifecycle.LiveData.Core.Ktx (>= 2.9.2.1)
- Xamarin.AndroidX.Lifecycle.Process (>= 2.9.2.1)
- Xamarin.GooglePlayServices.Auth (>= 121.4.0.1)
-
net10.0-ios26.0
- Microsoft.Maui.Controls (>= 10.0.1)
-
net10.0-maccatalyst26.0
- Microsoft.Maui.Controls (>= 10.0.1)
-
net10.0-windows10.0.19041
- Microsoft.Maui.Controls (>= 10.0.1)
- System.Management (>= 10.0.1)
-
net9.0
- Microsoft.Maui.Controls (>= 9.0.120)
-
net9.0-android35.0
- Microsoft.Maui.Controls (>= 9.0.120)
- Xamarin.AndroidX.Fragment.Ktx (>= 1.8.9)
- Xamarin.AndroidX.Lifecycle.LiveData (>= 2.9.2.1)
- Xamarin.AndroidX.Lifecycle.LiveData.Core (>= 2.9.2.1)
- Xamarin.AndroidX.Lifecycle.LiveData.Core.Ktx (>= 2.9.2.1)
- Xamarin.AndroidX.Lifecycle.Process (>= 2.9.2.1)
- Xamarin.GooglePlayServices.Auth (>= 121.4.0.1)
-
net9.0-ios18.0
- Microsoft.Maui.Controls (>= 9.0.120)
-
net9.0-maccatalyst18.0
- Microsoft.Maui.Controls (>= 9.0.120)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.