Search code examples
iosswiftxcodenfcmifare

Swift NFC Mifare - NFCISO7816APDU sendMifare Command not supported


I'm trying to write to a Mifare Ultralight NFC but I get this error:

Optional(Error Domain=NFCError Code=1 "Feature not supported" UserInfo={NSLocalizedDescription=Feature not supported})

I'm using this code for write:

if case let NFCTag.miFare(tag) = tags.first! {
            let dataMifare: [UInt8] = [240, 0, 0, 0] // READ page 4 + CRC
            let dataPacketMifare = Data(bytes: dataMifare, count: dataMifare.count)
            session.connect(to: tags.first!) { (error: Error?) in
let apdu = NFCISO7816APDU(instructionClass: 0xFF, instructionCode: 0xD6, p1Parameter: 0x00, p2Parameter: 0xFF, data: dataPacketMifare, expectedResponseLength: 0x02)
               
                
tag.sendMiFareISO7816Command(apdu) { (apduData, sw1, sw2, error) in
    let tagUIDData = tag.identifier
    session.invalidate(errorMessage: "Test")
    debugPrint(apduData)
    debugPrint(error)
    debugPrint(tag.identifier)

My Info.plist file:

<key>com.apple.developer.nfc.readersession.iso7816.select-identifiers</key>
    <array>
    <string>D2760000850100</string>
    <string>D2760000850101</string>
    </array>
<key>com.apple.developer.nfc.readersession.formats</key>
<array>
    <string>NDEF</string>
    <string>TAG</string>
</array>

I'm writing to a custom device where the value "0xFF" means a write request. If I change that value with "0x00" it works but for my device that is a read request

Am I doing something wrong or IOS 13.0 does not really support writing request on the Mifare?

I'm using Xcode 12.2, iOS 13.0 and Iphone 11.

Thanks in advance for the help

UPDATE 1 for Andrew reply

This is my code:

func handlerResponse(Result: Result< Data, Error>){
    print("error")
}

let dataMifareTest: [UInt8] = [0xF0, 0, 0, 0] 
let dataPacketMifareTest = Data(bytes: dataMifareTest, count: dataMifareTest.count)
tag.sendMiFareCommand(commandPacket: dataPacketMifareTest, resultHandler: handlerResponse)

But I get the following error:

NFCTagReader[516:101552] [CoreNFC] 00000002 83fd0090 -[NFCTagReaderSession transceive:tagUpdate:error:]:771  Error Domain=NFCError Code=100 "Tag connection lost" UserInfo={NSLocalizedDescription=Tag connection lost}

Solution

  • the problem is that you send has not the payload datas. The array [0xF0, 0, 0, 0] has only the page request. The error:

    NFCTagReader[516:101552] [CoreNFC] 00000002 83fd0090 -[NFCTagReaderSession transceive:tagUpdate:error:]:771  Error Domain=NFCError Code=100 "Tag connection lost" UserInfo={NSLocalizedDescription=Tag connection lost}
    

    is a malformed request.