Mau Ruiz
Mau Ruiz

Reputation: 777

Contact is missing some of the required key descriptors when displaying via CNContactViewController

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?

Upvotes: 4

Views: 1940

Answers (1)

vee_ess
vee_ess

Reputation: 699

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)

Upvotes: 5

Related Questions