Search code examples
iosswiftsecuritykeychain

How can I import a certificate into the iOS keychain?


I tried the following code but I keep getting "The specified attribute does not exist" error. kSecValueRef was checked and is not nil

let attributes: [String: Any] = [kSecClass as String : kSecClassCertificate,
                                 kSecValueRef as String : certificateRef!,
                                 kSecAttrApplicationTag as String : tag]

let osStatus = SecItemAdd(attributes as CFDictionary, nil)

Solution

  • You can find the resource describing available attributes for the kSecClassCertificate type here. You should change kSecAttrApplicationTag to kSecAttrLabel, that should solve the issue for you. The application tag is not a valid parameter for a certificate.

    p.s. There is a document describing each of these attributes per class on the Apple developer documentation page, or you could also look at the source code.