问题
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??
回答1:
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.
来源:https://stackoverflow.com/questions/58115985/nfc-ndef-writing-on-ios-encoding