How do I programmatically import a certificate into my iOS app's keychain and pass the identity to a server when needed?

后端 未结 2 1817
时光说笑
时光说笑 2021-02-04 05:39

I am working on an iOS5 application that will facilitate mobile payments between two users. As part of the payment process, the sender and the recipient need to communicate with

2条回答
  •  深忆病人
    2021-02-04 06:27

    Swift 4.0

     if let url = Bundle.main.url(forResource: "XXX", withExtension: "pem") {
    
                let PKCS12Data = NSData(contentsOf: url)
                let inPKCS12Data = CFDataCreate(kCFAllocatorDefault, PKCS12Data!.bytes.assumingMemoryBound(to: UInt8.self), (PKCS12Data?.length)!)
    
                let keys: [CFString] = [kSecImportExportPassphrase]
                let values: [CFTypeRef] = []
    
                let keysPointer = UnsafeMutablePointer.allocate(capacity: keys.count)
                keysPointer.initialize(to: keys)
    
                let valuesPointer = UnsafeMutablePointer.allocate(capacity: values.count)
                valuesPointer.initialize(to: values)
    
                let optionsDictionary = CFDictionaryCreate(kCFAllocatorDefault, keysPointer, valuesPointer, 1, nil, nil)
    
                var items = CFArrayCreate(kCFAllocatorDefault, UnsafeMutablePointer.allocate(capacity: 0), 0, nil)
                let securityError = SecPKCS12Import(inPKCS12Data!, optionsDictionary!, &items)
                if (securityError == 0) {
                    print("Certificate installed Successfully")
                } else {
                    print("Certificate installation failed")
                }
    
        }
    

提交回复
热议问题