Tutorial: your first NDEF scan on Android and iOS
Purpose: read the NDEF message of a tag from a .NET MAUI app, with the same shared code on Android and iOS. Platforms: Android, iOS. Status: stable.
At the end of this tutorial the app shows the first text record of a tag held near the phone. You need:
- the .NET SDK and MAUI workloads pinned by the repository (see CONTRIBUTING.md);
- an NFC-capable Android phone, or an NFC-capable iPhone with a development provisioning profile;
- an NTAG21x (NFC Forum Type 2) tag that contains a text record. You can write one with the SimpleApp or any NFC writer app.
1. Reference the library and register it
Add the Nementis.Maui.NFC package to the MAUI app project, then register the library in MauiProgram.cs:
using Nementis.Maui.NFC.Configuration;
builder.UseMauiApp<App>().UseNfc();
UseNfc registers INfcService as a singleton. Resolving it, or reading Capabilities, never starts the
radio or opens a system sheet.
2. Configure the Android host
Add the permission and the optional hardware feature to Platforms/Android/AndroidManifest.xml:
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="false" />
Reader mode, which this tutorial uses, needs no intent filters.
3. Configure the iOS host
In
Platforms/iOS/Info.plist, addNFCReaderUsageDescriptionwith a sentence that tells the user why the app reads tags.In
Platforms/iOS/Entitlements.plist, add the reader session formatTAG:<key>com.apple.developer.nfc.readersession.formats</key> <array> <string>TAG</string> </array>Enable NFC Tag Reading for the app identifier and sign with a provisioning profile that includes it.
Declare the same format when you register the library on iOS, instead of the plain
UseNfc()above:builder.UseNfc(new Nementis.Maui.NFC.IosExtensions.IosNfcIntegrationOptions { ReaderSessionFormats = ["TAG"] });
4. Read the tag
Inject INfcService into the page and call ScanOnceAsync. It starts a session, runs the operation on the
first discovered tag and always closes the session:
var prompt = new NfcScanPrompt(Scanning: "Hold your tag near the phone.",
Success: "Tag read.", Failure: "The tag could not be read.");
var read = await nfc.ScanOnceAsync(new NfcScanOptions { Prompt = prompt },
(tag, token) => tag.GetProtocol<INfcNdefTag>() is { } ndef
? ndef.ReadAsync(token)
: Task.FromResult(NfcResult<NfcNdefData>.Failure(
NfcOperationContext.Create(tag.SessionId, tag.Id),
new(NfcErrorCode.Unsupported, "app.ndef.missing"))),
TimeSpan.FromSeconds(30), cancellationToken);
On iOS the system sheet shows Scanning, then Success or Failure. Android has no system sheet: show the
prompt in your own UI.
5. Show the result
Every expected failure, including cancellation and timeout, is a typed result rather than an exception:
if (!read.IsSuccess) {
Status.Text = $"No data: {read.Error!.PortableReasonCode}";
return;
}
var record = read.Value.Message?.Records.FirstOrDefault();
var text = record is null ? null : NfcNdefRtd.DecodeTextRecord(record);
Status.Text = text is { IsSuccess: true } ? text.Value.Text : "The tag has no text record.";
Run the app on the device, start the scan and hold the tag near the phone: the first text record appears.
Next steps
- Write to a tag and confirm the result: write NDEF with verification.
- Understand why a second scan can return
Busy: ownership. - See what each platform supports: capability matrix.