Skip to content

Instantly share code, notes, and snippets.

@FreakyAli
Created August 23, 2022 16:30
Show Gist options
  • Save FreakyAli/d6872bfc3c51367e95941e5c4410cebe to your computer and use it in GitHub Desktop.
Save FreakyAli/d6872bfc3c51367e95941e5c4410cebe to your computer and use it in GitHub Desktop.
NFC service for iOS
using System;
using CoreNFC;
using NDEF.MAUI.Enums;
using NDEF.MAUI.Interfaces;
using UIKit;
public class NfcService : INfcService
{
public async Task SendAsync(byte[] bytes)
{
var isNfcAvailable = UIDevice.CurrentDevice.CheckSystemVersion(11, 0);
if (isNfcAvailable && NFCNdefReaderSession.ReadingAvailable)
{
await MainThread.InvokeOnMainThreadAsync(async () =>
{
try
{
var sessionDelegate = new SessionDelegate(bytes);
var session = new NFCNdefReaderSession(sessionDelegate, null, true);
session.BeginSession();
var status = await sessionDelegate.WasDataTransmitted.Task;
if (status != NfcTransmissionStatus.Success)
{
await Application.Current.MainPage.DisplayAlert("Error", "Suitable error message", "Ok");
}
}
catch
{
await Application.Current.MainPage.DisplayAlert("Error", "There was an error while trying to create a NFC session", "Ok");
}
});
}
else
{
await Application.Current.MainPage.DisplayAlert("Error", "Read is not supported by this tag", "Ok");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment