Preeti
Preeti

Reputation: 753

How to communicate with USB-C smart card reader in iOS/iPadOS?

I am quite new to iOS programming. I am trying to communicate with a CCID-compliant USB-C smartcard reader using the TKSmartCardSlotManager interface of the CryptoTokenKit framework. I am building this for ios 16.0 and testing it on ipad mini that has a USB-C connector. Below is the sample code I am testing with:

import CryptoTokenKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        // Start observing smart card slot state changes
        startDetectingSmartCard()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        // Stop observing smart card slot state changes when the view disappears
        stopDetectingSmartCard()
    }

    func startDetectingSmartCard() {
        DispatchQueue.global().async {
            if let slotManager = TKSmartCardSlotManager.default {
                while true {
                    let slotNames = slotManager.slotNames
                    
                    for reader in slotNames {
                        slotManager.getSlot(withName: reader) { slot in
                            guard let slot = slot else {
                                print("Slot for reader \(reader) is nil")
                                return
                            }
                            
                            let state = slot.state
                            if state.contains(.cardPresent) {
                                print("Smart card is present in slot: \(reader)")
                                
                                // Example: Communicate with the smart card
                                if let card = slot.makeSmartCard() {
                                    card.beginSession { session, error in
                                        if let error = error {
                                            print("Error beginning session with smart card: \(error.localizedDescription)")
                                            return
                                        }
                                    }
                                }
                            } else if state.contains(.empty) {
                                // Smart card slot is empty
                                print("Smart card slot is empty: \(reader)")
                            } else {
                                print("Smart card slot is in an unknown state: \(reader)")
                            }
                        }
                    }
                    
                    // Poll every few seconds
                    sleep(5)
                }
            } else {
                print("TKSmartCardSlotManager.default is nil")
            }
        }
    }

    func stopDetectingSmartCard() {
    }
}

Under keychain aceess group entitlement, I have added com.apple.token and com.apple.security.smartcard (I think this is relevant only for macOS).

When the code is built for macOS, it can detect the smartcard and read the ATR also. However, when built for iOS and testing with iPad it cannot detect the smartcard. With very little information about this in the internet and apple support channel, I could not debug why this is not working.

Can anyone please let me know what is the issue here and how to resolve it?

Thanks in advance.

P.S: The smartcard reader is not MFi certified. Please let me know if any info is missing.

Upvotes: 0

Views: 200

Answers (0)

Related Questions