4

Trying to display a contact with the prebuilt UI in a given tableView, when the user selects the contact to display the following error appears:

CNPropertyNotFetchedException', reason: 'Contact 0x7fded8ee6f40 is missing some of the required key descriptors: [CNContactViewController descriptorForRequiredKeys]>

I already tried to solve by this method: Contact is missing some of the required key descriptors in ios

So my contact array creation is as follows:

  func searchContactDataBaseOnName(name: String) {
        results.removeAll()

        let predicate = CNContact.predicateForContactsMatchingName(name)
        //Fetch Contacts Information like givenName and familyName
        let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactViewController.descriptorForRequiredKeys()]

        let store = CNContactStore()        
        do {
            let contacts = try store.unifiedContactsMatchingPredicate(predicate,
                keysToFetch: keysToFetch)                
            for contact in contacts {
                self.results.append(contact)
            }
            tableContacts.reloadData()
        }
        catch{
            print("Can't Search Contact Data")
        }
    }

And when the user taps on a row index, I'm trying to display by doing this:

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

        let viewControllerforContact = CNContactViewController(forContact: results[indexPath.row])
        viewControllerforContact.contactStore = self.contactStore
        viewControllerforContact.delegate = self

        self.navigationController?.pushViewController(viewControllerforContact,animated:true)
    }

Any ideas on how to solve? It seems that I'm still missing to pass the descriptorForRequiredKeys to the array "Results"... Maybe?

1 Answer 1

5

You are fetching the required keys and storing them in the results variable you are calling from so I don't know the cause. You can re-fetch the contact with just required keys to get around the error:

var contact = results[indexPath.row]    
if !contact.areKeysAvailable([CNContactViewController.descriptorForRequiredKeys()]) {
    do {
        contact = try self.contactStore.unifiedContactWithIdentifier(contact.identifier, keysToFetch: [CNContactViewController.descriptorForRequiredKeys()])
    }
    catch { }
}
let viewControllerforContact = CNContactViewController(forContact: contact)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.