1

I've application for NFC write in swift 5, Write is working properly but I want to update only 1 key from nfc without overwrite full nfc card data. NFC device is ISO 14443-3A NXP MIFARE Ultralight (Ultralight C). Please check code below and anybody can help on this.

 var payloadData = Data([0x02,0x65,0x6E])
  payloadData.append(self.record_to_write.data(using: .utf8)!)
                                    
                                let customTextPayload = NFCNDEFPayload.init(
                                   format: .nfcWellKnown,
                                   type: "T".data(using: .utf8)!,
                                   identifier: Data(),
                                   payload:payloadData
                                )
                                self.record_new_arr.append(customTextPayload)
let messge = NFCNDEFMessage.init(records: self.record_new_arr )
tag.writeNDEF(messge, completionHandler: { (error: Error?) in
            if nil != error{
                session.alertMessage = "Write NDEF message fail: \(error)"
                print("Write NDEF message fail: \(error)")
            }else{
                print("Write NDEF message successfull.")
                session.alertMessage = "Write NDEF message successfull."
            }

        })

Only need to prevent overwrite data into nfc using swift. Thanks in advance

1 Answers1

1

NDEF format is not really designed to appended to or updated directly on the card.

Because of the structure of the data using TLV records
T = Terminator (1 Byte)
L = Length (1+ Bytes)
V = Value (0+ Bytes) (this is the data to be stored)

and then another Terminator

Then in a sizeable number of cases the to append/update you would have to re-write the whole card or some large portion of the card anyway because the length would likely to need to be updated.
There are only a limited few cases where and replace existing data would make sense.

Therefore most implementation will only write a complete NDEF message, overwriting what already exists on the Card because to make updates you would have to read the whole message anyway.

The only way to update a NDEF message in read it in to your program, update/append records in memory, write the whole thing back to the card.

So in short what you are asking is not possible (while in a very limited case it might be possible for you to work out the changes yourself and make changes on a Type 2 card, but this would not work on other card types)

Andrew
  • 8,198
  • 2
  • 15
  • 35
  • Thank you but I've already fetch all record then write entire record but records are not write using tag.writeNDEF in card. my card details is : Tag Type: ISO 14443-3A, technology: Type A, Mifare ultralight. **tag.writeNDEF** works in other card – 99 Degree Info Jul 04 '20 at 11:49