0

I want to fetch all contacts stored that has no names on it. The below method helps me to search for all the contacts with string passed. I tried to pass empty string to find all contacts with no name, it didn't work.

func fetchContactsWithNoName() {

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

    // Iterate all containers and append their contacts to our results array
    for _ in allContainers {
      //  let fetchPredicate = CNContact.predicateForContactsInContainer(withIdentifier: container.identifier)

        do {
            let containerResults =  try contactStore.unifiedContacts(matching: CNContact.predicateForContacts(matchingName: " "), keysToFetch:[CNContactGivenNameKey as CNKeyDescriptor, CNContactFamilyNameKey as CNKeyDescriptor])
            self.results.append(contentsOf: containerResults)

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

}
halfer
  • 19,824
  • 17
  • 99
  • 186
Warrior
  • 39,156
  • 44
  • 139
  • 214

0 Answers0