I am trying get NSData object with the vCard representation of the contact. My code:
let contactStore = CNContactStore()
let fetchRequest = CNContactFetchRequest(keysToFetch: [CNContactGivenNameKey, CNContactFamilyNameKey])
var contacts = [CNContact]()
var vcard = NSData()
do{
try contactStore.enumerateContactsWithFetchRequest(fetchRequest) { (contact, status) -> Void in
self.fetchRequest.unifyResults = true
self.contacts.append(contact)}
} catch {
print("Error \(error)")
}
do {
try vcard = CNContactVCardSerialization.dataWithContacts(contacts)
} catch {
print("Error \(error)")
}
But, i get error:
Exception writing contacts to vCard (data): A property was not requested when contact was fetched. Error NilError.
I understand that the error in the access to contacts, but how to fix it?