Search code examples
iosswiftvcf-vcardcncontact

CNContactVCardSerialization.dataWithContacts giving exception


I'm trying to convert a CNContact array to vCard using the method CNContactVCardSerialization.dataWithContacts(). But it is giving me the following error.

2016-07-25 14:05:00.115 AddressBook-ios9[902:28918] Exception writing contacts to vCard (data): A property was not requested when contact was fetched.

I made sure that I'm passing an valid array of CNContacts, but still it is giving this exception. Can anybody guide to me to what I've done wrong?

I'm attaching the source code below.

func getVcardFromSearchingName(name: String) -> NSData? {
    do {            
        if let contacts = searchMultiContacts(name) {
            print(contacts)
            let vCard = try CNContactVCardSerialization.dataWithContacts(contacts)
            return vCard
        } else {
            return nil
        }
    } catch {
        return nil
    }
}

Solution

  • I found out my mistake. On the keys to fetch contact, I was missing CNContactVCardSerialization.descriptorForRequiredKeys(). After adding it, the code is working flawlessly.