We are using the CoreNFC framework in iOS 13 to read and write to a MiFare Ultralight NFC tag. We have no problems reading the tag and getting the data out from it, but we have a problem when trying to write to it when password protection is enabled in the tag.
If we use a standard MiFare Ultralight tag we can write to it without problems. But as soon as we use a password protected tag, we cannot write to the tag anymode. (The password is known) And we need to find out how to unlock the tag with the password.
When we try to write to the tag, we do the following steps:
- We use the NFCTagReaderSession to read the tag.
- Then we use a switch statement on the tag to get the type of it, where we get .miFare.
- Then we connect the session to the tag.
- Then we try to unlock it using the tag.sendMiFareCommand with the a UInt8 array starting with the “0x1B” auth command byte followed by the 4 bytes password.
- Then we check the response data and compare it to the PACK which is programmed into the tag, which matches.
- Then we call the tag.queryNDEFStatus function to make sure, that the tag is having the status .readWrite.
- Then we check if the message.length is less than the capacity of the tag, which it is.
- Finally we call the tag.writeNDEF function with an NDEF Message which is an exact copy of the one already on the tag. From this call we get the error: “Stack Error” Underlying error: “Connection Closed”.
- If we follow the approach above with a similar tag just without a password, and skipping the unlocking step, the write is successful.
Questions:
- What are the correct approach in your opinion to unlocking a tag with a password using the CoreNFC framework - we do not see any functions mentioning passwords for mifare tags?
- Is there a function in CoreNFC to unlock a password protected tag, or is sendMiFareCommand the right way to do it?
- Why do we get a “Connection closed” error if the password is the error?