NFC NDEF writing on IOS Encoding

会有一股神秘感。 提交于 2020-08-10 03:31:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!