I am trying to retrieve all contacts and save them in VCard form (swift 4, XCode 9.0). But CNContactVCardSerialization.data(with:) always returns nil. Here is my code:
var contacts = [CNContact]()
let request = CNContactFetchRequest(keysToFetch:[CNContact.descriptorForAllComparatorKeys()])
do {
try contactsStore.enumerateContacts(with: request, usingBlock:
{ (contact:CNContact, result:UnsafeMutablePointer<ObjCBool>) in
self.contacts.append(contact)
})
}
catch {
}
// at this point all contacts are in the "contacts" array.
var data = Data()
do {
try data = CNContactVCardSerialization.data(with: contacts)
}
catch {
print("some error in contacts:" + String(describing: error));
}
print(">>>data:" + String(data.count))
Output:
2017-11-02 XXX [5224:449081]
Exception writing contacts to vCard (data): A property was not
requested when contact was fetched.
2017-11-02 XXX [5224:449362] XPC
connection interrupted
some error in contacts:nilError
>>>data:0
I red the question below but it does not help. How to use method dataWithContacts in CNContactVCardSerialization?
I added "Privacy - Contacts Usage Description" into info.plist