How to import DER certificate into swift?

So, it seems that a bunch of methods that broke things in my current codebase have changed. I am currently getting the following error:

Cannot convert expression type '(CFAllocator !, data: @lvalue NSData)' to enter 'CFData!'

Here is the relevant code:

let mainbun = NSBundle.pathForResource("mainkey", ofType: "der", inDirectory: "/myapppath")
var key: NSData = NSData(base64EncodedString: mainbun!, options: nil)!
var turntocert: SecCertificateRef = SecCertificateCreateWithData(kCFAllocatorDefault, data: key)

It works for me with the bridge header, but I still would like to just create a certificate link directly in swift.


UPDATE: it works

var bundle: NSBundle = NSBundle.mainBundle()
var mainbun = bundle.pathForResource("keyfile", ofType: "der")
var key: NSData = NSData(contentsOfFile: mainbun!)!
var turntocert: SecCertificateRef =
SecCertificateCreateWithData(kCFAllocatorDefault, key).takeRetainedValue()
+4
source share
1 answer

In Swift, SecCertificateCreateWithDatareturns a type Unmanaged. You need to get the value of the unmanaged link with takeRetainedValue().

let mainbun = NSBundle.pathForResource("mainkey", ofType: "der", inDirectory: "/myapppath")
var key: NSData = NSData(base64EncodedString: mainbun!, options: nil)!
var turntocert: SecCertificateRef = 
    SecCertificateCreateWithData(kCFAllocatorDefault, key).takeRetainedValue()

, , CFData NSData. .

+4

All Articles