通过CNContactViewController显示时,联系人缺少一些必需的键描述符

时间:2016-02-23 21:08:38

标签: ios iphone xcode swift

当用户选择要显示以下错误的联系人时,尝试在给定的tableView中显示与预建UI的联系人:

  

CNPropertyNotFetchedException',原因:'联系人0x7fded8ee6f40是   缺少一些必需的键描述符:[CNContactViewController   descriptorForRequiredKeys] GT;

我已经尝试通过这种方法解决:Contact is missing some of the required key descriptors in ios

所以我的联系人数组创建如下:

  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")
        }
    }

当用户点击行索引时,我试图通过这样做来显示:

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)
    }

关于如何解决的任何想法?似乎我仍然缺少将descriptorForRequiredKeys传递给数组“结果”......也许?

1 个答案:

答案 0 :(得分:3)

正在获取所需的密钥并将它们存储在你正在调用的结果变量中,所以我不知道原因。您可以使用所需的密钥重新获取联系人以解决错误:

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)