CNContacts未从联系人列表中

时间:2018-06-05 05:11:05

标签: ios swift

CNContacts不从联系人列表中提取所有联系人。有些联系人遗失了。任何人都可以提供帮助。

这是我的代码。

    let key = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey] as [CNKeyDescriptor]
    let request = CNContactFetchRequest(keysToFetch: key)
    try! contactStore.enumerateContacts(with: request) { (contact, stoppingPointer) in
        let givenName = contact.givenName
        let familyName = contact.familyName
        print(contact.phoneNumbers[0].label!)
        var number = [contact.phoneNumbers[0].value.stringValue]
        var type = [contact.phoneNumbers[0].label]
        if contact.phoneNumbers.count > 1{
            for i in 1...contact.phoneNumbers.count - 1{
                number.append(contact.phoneNumbers[i].value.stringValue)
                type.append(contact.phoneNumbers[i].label)
            }
        }
        let imagedata = contact.imageData
        let contactToAppend = ContactStruct(givenName: givenName, familyName: familyName, number: number, type: type as! [String] , image: imagedata)
        self.contacts.append(contactToAppend)
    }

1 个答案:

答案 0 :(得分:0)

请尝试以下代码,希望它有效。

func getContactList() {

  let contacts = self.getContactFromCNContact()

  for contact in contacts {

     //do your stuff with contact
  }
}

func getContactFromCNContact() -> [CNContact] {

    let contactStore = CNContactStore()
    let keysToFetch = [
        CNContactFormatter.descriptorForRequiredKeys(for: .fullName),
        CNContactIdentifierKey,
        CNContactNamePrefixKey,
        CNContactGivenNameKey,
        CNContactMiddleNameKey,
        CNContactFamilyNameKey,
        CNContactPreviousFamilyNameKey,
        CNContactNameSuffixKey,
        CNContactNicknameKey,
        CNContactOrganizationNameKey,
        CNContactDepartmentNameKey,
        CNContactJobTitleKey,
        CNContactPhoneticGivenNameKey,
        CNContactPhoneticMiddleNameKey,
        CNContactPhoneticFamilyNameKey,
        CNContactBirthdayKey,
        CNContactNonGregorianBirthdayKey,
        CNContactNoteKey,
        CNContactImageDataKey,
        CNContactThumbnailImageDataKey,
        CNContactImageDataAvailableKey,
        CNContactTypeKey,
        CNContactPhoneNumbersKey,
        CNContactEmailAddressesKey,
        CNContactPostalAddressesKey,
        CNContactDatesKey,
        CNContactUrlAddressesKey,
        CNContactRelationsKey,
        CNContactSocialProfilesKey,
        CNContactInstantMessageAddressesKey] as [Any]

    //Get all the containers
    var allContainers: [CNContainer] = []
    do {
        allContainers = try contactStore.containers(matching: nil)
    } catch {
        print("Error fetching containers")
    }

    var results: [CNContact] = []

    // Iterate all containers and append their contacts to our results array
    for container in allContainers {

        let fetchPredicate = CNContact.predicateForContactsInContainer(withIdentifier: container.identifier)

        do {
            let containerResults = try contactStore.unifiedContacts(matching: fetchPredicate, keysToFetch: keysToFetch as! [CNKeyDescriptor])
            results.append(contentsOf: containerResults)

        } catch {
            print("Error fetching results for container")
        }
    }

    return results
}