简体   繁体   中英

How to install certificate programmatically in IOS

I'm trying to install a certificate programmatically in IOS. I have successfully added the certificate using the following code :

let rootCert = SecCertificateCreateWithData(kCFAllocatorDefault,data_array as NSData)
var err: OSStatus = noErr
let dict = NSDictionary.init(objects: [kSecClassCertificate, rootCert!], forKeys: [kSecClass as! NSCopying, kSecValueRef as! NSCopying])
err = SecItemAdd(dict, nil)
if(err == noErr) {
    NSLog("Install root certificate success");
} else if( err == errSecDuplicateItem ) {
    NSLog("duplicate root certificate entry");
} else {
    NSLog("install root certificate failure");
}

But I can't see the my certificate in Certificate Trust Settings .I'm excepting to see my certificate in setting with untrusted status then allow the user to trust my certificate.

Is this the correct way to add the certificate? If not how can certificates be installed correctly in IOS? Is there a way to prompt the user to set the installed certificate as fully trusted.

What you want to do is not possible. Allowing apps to install certificates and even mark them as fully trusted would be a huge security risk.

The code you are using is to create and add a certificate to the app's keychain, which is entirely unrelated to the system certificate store.

Your only option is to direct your users somewhere where they can install the certificate. I do not know the context your app is going to operate in (is it an enterprise app? App for end users?), but I will assume you know the implications of asking users to install certificates they may not trust.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM