连接到NFCTagReaderSession NFCISO7816Tag标记并在iOS 13中发送命令

时间:2019-10-01 15:53:31

标签: ios swift nfc ios13 core-nfc

我正在研究NFCTagReaderSession,这是iOS 13中的新功能,我想连接会话标签并发送apdu命令进行通信。

当我调用connect属性时,它看起来很像15秒的连接时间(发出哔哔声)时显示连接错误

  

NFCError Code = 201“会话超时”。

每次tagReaderSession:didInvalidateWithError在连接卡时都在通话,而我无法发送apdu命令。

我尝试连接并发送apudu命令的代码

var nfcSession: NFCTagReaderSession?  
nfcSession = NFCTagReaderSession(pollingOption: [.iso14443],      delegate: self, queue: DispatchQueue.main)
nfcSession?.alertMessage = "Hold your iPhone near an NFC."
nfcSession?.begin()

// MARK: - NFCTagReaderSessionDelegate

public func tagReaderSessionDidBecomeActive(_ session: NFCTagReaderSession) {
    print("tagReaderSessionDidBecomeActive")
}

public func tagReaderSession(_ session: NFCTagReaderSession, didInvalidateWithError error: Error) {
    print( "tagReaderSession:didInvalidateWithError - \(error)" )
}

public func tagReaderSession(_ session: NFCTagReaderSession, didDetect tags: [NFCTag]) {

let tag = tags.first!
var nfcTag7816: NFCISO7816Tag
switch tags.first! {
 case let .iso7816(tag):
     nfcTag7816 = tag
   @unknown default :
        session.invalidate(errorMessage: "Tag not valid.")
             return
   }

 session.connect(to: tags.first!) { (error: Error?) in
     if error != nil {
        session.invalidate(errorMessage: "Connection error. Please try again.")
           return
    }
    else {
        let myAPDU = NFCISO7816APDU(instructionClass:0, instructionCode:0xB0, p1Parameter:0, p2Parameter:0, data: Data(), expectedResponseLength:16)
         nfcTag7816.sendCommand(apdu: myAPDU) { (response: Data, sw1: UInt8, sw2: UInt8, error: Error?)
                     in

        guard error != nil && !(sw1 == 0x90 && sw2 == 0) else {
               session.invalidate(errorMessage: "Applicationfailure")
            return
            }
          }
       }
   }
}

连接时发现错误:

tagReaderSession:didInvalidateWithError - Error Domain=NFCError Code=201 "Session timeout" UserInfo={NSLocalizedDescription=Session timeout}

请告诉我发生此错误的确切原因,以便我可以根据解决方案更改代码

0 个答案:

没有答案