I am working on a flutter app which reads and writes to NFC-tags. On Android it works just fine. Now I am working on implementing NFC-writing on IOS since it is now available.
I do get the app to write a text-record to the tag. But somehow it is written in UTF16 which makes things break apart.
How do I create NFCNDEFPayload.wellKnownTypeTextPayload with a string that is UTF-8 encoded??
You got most of the way there. Here's a full working example which fixes a couple things (specifically missing 'type' specifier and locale usage):
var payloadData = Data([0x02,0x65,0x6E]) // 0x02 + 'en' = Locale Specifier
payloadData.append("Text To Write".data(using: .utf8)!)
let payload = NFCNDEFPayload.init(
format: NFCTypeNameFormat.nfcWellKnown,
type: "T".data(using: .utf8)!,
identifier: Data.init(count: 0),
payload: payloadData,
chunkSize: 0)
Note that this sets the locale to 'en'.
I've tested tags created with this code on both iPhone and Android, and it seems to be working fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With