Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 375
Mau Ruiz Avatar asked Nov 17 '25 03:11

Mau Ruiz


1 Answers

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)
like image 187
vee_ess Avatar answered Nov 18 '25 20:11

vee_ess



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!