Connect to NFCTagReaderSession NFCISO7816Tag tag and send command in iOS 13

穿精又带淫゛_ 提交于 2021-01-28 05:50:50

问题


I am working on NFCTagReaderSession the newly available in iOS 13, i struck to connect the session tag and send apdu commands for communication.

when i call the connect property it looks long time like 15 secs to connect by the time when it is connected(beeps sound) it shows an error message

NFCError Code=201 "Session timeout".

Every time tagReaderSession:didInvalidateWithError is calling while connecting the card and i could not send apdu commands.

the code which i tried to connect and send apudu command

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
            }
          }
       }
   }
}

Error found when connected:

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

Please suggest me the exact reason of happening this one, and so that i can change in code according to the solutions

来源:https://stackoverflow.com/questions/58188447/connect-to-nfctagreadersession-nfciso7816tag-tag-and-send-command-in-ios-13

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!