Post not yet marked as solved
Post marked as unsolved with 0 replies, 88 views
Hello
iPhone xs for testing
XCode 15.3
I've successfully managed to select the AID and send other commands to a SmartCard using the following code:
class NFCReader: NSObject, ObservableObject, NFCTagReaderSessionDelegate {
func startNFCSession() {
session = NFCTagReaderSession(pollingOption: [.iso14443], delegate: self, queue: nil)
session?.alertMessage = "Hold your iPhone near the NFC tag."
session?.begin() // Start the session
}
func tagReaderSession(_ session: NFCTagReaderSession, didDetect tags: [NFCTag]) {
guard let tag = tags.first else { // Safely unwrap the first tag
session.invalidate(errorMessage: "No SmartCard detected.")
return
}
session.connect(to: tag, completionHandler: { (error: Error?) in
if let error = error {
session.invalidate(errorMessage: "Unable to connect to SmartCard: \(error.localizedDescription)")
return
}
switch tag {
case let .iso7816(iso7816Tag):
self.sendAPDUCommandSelect(to: iso7816Tag)
self.sendAPDUCommand_2(to: iso7816Tag)
default:
print("Not iso7816 type") // Update the SwiftUI view
break
}
})
}
}
I encounter an issue when attempting to interact with the user for specific commands. I need to maintain the session active in the background while waiting for additional commands. However, upon closing the NFC dialog, I receive the NFCError Code=200 "Session invalidated by the user" error.
My ideal use case:
Button1: Initiates the connection and waits for the SmartCard to be presented.
tagReaderSession() connects to the SmartCard and sends the AID.
Button2: Sends APDU command 2.
Button3: Sends APDU command 3.
To proceed with clicking the other buttons, the NFC dialog must be closed, but doing so invalidates the session with error 200.
Is there a way to keep the connection valid in the background while waiting for additional commands?