How to Convert Unmanaged<Cfdata> to Nsdata

How to convert unmanaged CFData to NSData?

As explained in
Working with Cocoa Data Types, you have to convert the unmanaged object to a memory managed object with takeUnretainedValue() or takeRetainedValue().
In your case

if (ABPersonHasImageData(person)) {
let imgData = ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatOriginalSize).takeRetainedValue()
let image = UIImage(data: imgData)
}

because ABPersonCopyImageDataWithFormat() returns a (+1) retained value.

How do you import a DER certificate in swift?

In Swift, SecCertificateCreateWithData returns an Unmanaged type. You need to get the value of the unmanaged reference 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()

The core problem you have is converting CFData to NSData. See this question

Error with fetching value from CFArray

You cannot force cast an UnsafePointer<Void> to a type. You must first convert that void pointer to UnsafePointer<Type> then take its memory:

let aPerson = UnsafePointer<ABRecordRef>(CFArrayGetValueAtIndex(allPeople, i)).memory

FYI... ABAddressBook has been deprecated on iOS 9. For new code targeting that OS, use CNContactStore instead.

Cannot convert value of type 'SecCertificate' to expected argument type 'UnsafeMutablePointer UnsafeRawPointer? !'

The line causing the error, is for creating a CFArray.

One simple way to create a CFArray, is to use bridging the Swift Array to CFArray.

Try changing the line:

let certArrayRef = CFArrayCreate(nil, cert, 1, nil)

To:

let certArrayRef = [cert] as CFArray


Related Topics



Leave a reply



Submit