I've written a small test app to bring my understanding of CoreNFC up to snuff. The app just reads a tag and writes a simple string to it. The app has NFC entitlement, NFC privacy statement as required.
The app reports the tag has read/write capability.
When I read the tag message with NXP's 'TagWriter' app it states "This is a test" + current date. This is what I expect. When I read it with the app I wrote I get two chinese characters obscuring the first character i.e. capital 'T'. E.g. "<chinese>his is a test 2019-12-18 03:27:08". This is the method I use to extract the text:
func printNDEF(_ messages:[NFCNDEFMessage]) {
DispatchQueue.main.async {
for message in messages {
for record in message.records {
if let string = String(data: record.payload, encoding: .utf16) {
print("MSG:\(string)")
}
}
}
}
}
So please enlighten me what I do wrong here? Why is that first 'T' replaced by chinese whereas the NXP app does this correctly?
Then when I write to the tag the same string but with updated date/time. I get an error '92', LLDB reports; [CoreNFC] 00000002 83870f00 -[NFCNDEFReaderSession setAlertMessage:]:92 This is the method I use:
func doWriteTest(_ session: NFCNDEFReaderSession, tag:NFCNDEFTag) {
print("Write Test")
guard let record = NFCNDEFPayload.wellKnownTypeTextPayload(string: " This is a test \(Date())", locale: .current) else {
return
}
let myMessage = NFCNDEFMessage.init(records: [record])
tag.writeNDEF(myMessage) { (error) in
if let error = error {
session.alertMessage = "Write NDEF message fail: \(error)"
}
else {
session.alertMessage = "Write NDEF message successful!"
}
}
}
The method reports it was successful and the tag is indeed written to with the new date/time. No idea why I get error 92 and what it means. Note that I do have a session alert message, the NFC session is started with this:
func createNFCSession() {
session = NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: false)
session?.alertMessage = "Hold phone near tag."
session?.begin()
}
Any help is much appreciated.