Sample code for 30+ languages & platforms
Swift

Apple Keychain - List Certs on Smartcards and USB Tokens

See more Apple Keychain Examples

Iterates over the certificatse on connected smartcards and USB tokens via the Apple Keychain.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    let certStore = CkoCertStore()!

    // On MacOS and iOS, the OpenSmartcard method opens the Keychain.
    // The argument passed to OpenSmartcard is ignored.
    success = certStore.openSmartcard(csp: "")
    if success == false {
        print("\(certStore.lastErrorText!)")
        return
    }

    var numCerts: Int = certStore.numCertificates.intValue
    print("numCerts = \(numCerts)")

    let cert = CkoCert()!
    var i: Int = 0
    while i < numCerts {
        // Note: Chilkat also gets the associated private key if it exists.
        // You can simply use the cert in other places in Chilkat where a cert w/ private key is required.
        certStore.getCert(index: i, cert: cert)
        print("\(cert.subjectDN!)")
        print("\(cert.subjectCN!)")
        print("\(cert.serialNumber!)")
        if cert.isRsa() == true {
            print("key type is RSA")
        }

        if cert.isEcdsa() == true {
            print("key type is ECDSA")
        }

        print("has private key: \(cert.hasPrivateKey())")
        print("----")
        i = i + 1
    }

    certStore.close()

}