Hi, any help is appreciated please, many thanks in advance !
In my python script, I can get the CPLC data of a smartcard with the APDU: 80 CA 9F 7F 00.
But on the iOS side with CoreNFC, the same exact APDU and card is giving a response of 0x6D00.
Was wondering possible causes of this, or if I'm overlooking an error in my code:
import SwiftUI
import CoreNFC
struct ContentView: View {
@State private var responseText: String = "Response will appear here."
@State private var nfcSession: NFCTagReaderSession?
@State private var nfcDelegate: NFCDelegate?
var body: some View {
VStack {
Text(responseText)
.padding()
.multilineTextAlignment(.center)
Button("Start NFC Session") {
startNFCSession()
}
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
.padding()
}
func startNFCSession() {
guard NFCTagReaderSession.readingAvailable else {
responseText = "NFC not supported"
return
}
let delegate = NFCDelegate(responseText: $responseText)
nfcDelegate = delegate
nfcSession = NFCTagReaderSession(pollingOption: [.iso14443], delegate: delegate, queue: nil)
nfcSession?.alertMessage = "Hold card up on the back"
nfcSession?.begin()
}
}
class NFCDelegate: NSObject, NFCTagReaderSessionDelegate {
@Binding var responseText: String
init(responseText: Binding<String>) {
_responseText = responseText
}
deinit {
print("NFCDelegate instance is being deallocated")
}
func tagReaderSessionDidBecomeActive(_ session: NFCTagReaderSession) {
print("tag readers active")
}
func tagReaderSession(_ session: NFCTagReaderSession, didInvalidateWithError error: Error) {
print("Session invalidated with error")
DispatchQueue.main.async {
self.responseText = "Session invalidated: \(error.localizedDescription)"
}
}
func tagReaderSession(_ session: NFCTagReaderSession, didDetect tags: [NFCTag]) {
print("didDetect method called with \(tags.count) tags")
guard tags.count == 1 else {
session.invalidate(errorMessage: "More than one tag detected, please try again.")
return
}
let tag = tags.first!
session.connect(to: tag) { error in
if let error = error {
session.invalidate(errorMessage: "Connection failed: \(error.localizedDescription)")
return
}
print("Connection successful")
switch tag {
case .iso7816(let iso7816Tag):
print("ISO7816 Tag detected")
let apdu = NFCISO7816APDU(
instructionClass: 0x80,
instructionCode: 0xCA,
p1Parameter: 0x9F,
p2Parameter: 0x7F,
data: Data(),
expectedResponseLength: -1
)
var apduBytes: [UInt8] = []
apduBytes.append(apdu.instructionClass)
apduBytes.append(apdu.instructionCode)
apduBytes.append(apdu.p1Parameter)
apduBytes.append(apdu.p2Parameter)
let apduString = apduBytes.map { String(format: "%02X", $0) }.joined(separator: " ")
print("APDU: \(apduString)")
iso7816Tag.sendCommand(apdu: apdu) { response, sw1, sw2, error in
if let error = error {
session.invalidate(errorMessage: "APDU command failed: \(error.localizedDescription)")
return
}
let responseString = response.map { String(format: "%02hhX", $0) }.joined()
DispatchQueue.main.async {
print("APDU Response: \(responseString) SW1: \(String(format: "%02X", sw1)) SW2: \(String(format: "%02X", sw2))")
self.$responseText.wrappedValue = "APDU Response: \(responseString) SW1: \(sw1) SW2: \(sw2)"
}
session.invalidate()
}
default:
session.invalidate(errorMessage: "Unsupported tag type.")
}
}
}
}