Not able to search for local contacts with CNContactStore()

We are developing an app that uses the local contact search functionality. However, we are encountering an issue where some users are unable to search for local contacts using the contact's name or number input.

Below is a code snippet from our project that uses the enumerateContacts() API from CNContactStore():

public func searchContacts(query: String, completion: @escaping SearchContactHandler) {
    let keysToFetch: [CNKeyDescriptor] = [
        CNContactFormatter.descriptorForRequiredKeys(for: .fullName),
        CNContactPhoneNumbersKey as CNKeyDescriptor,
    ]
    DispatchQueue.global(qos: .userInitiated).async { [store] in
        do {
            var contacts: [PhoneContactInfo] = []
            try store.enumerateContacts(with: CNContactFetchRequest(keysToFetch: keysToFetch)) { contact, _ in
                contacts.append(contentsOf: PhoneContactsFilter.filter(contact: contact, query: query))
            }
            completion(.success(contacts))
        } catch {
            completion(.failure(error))
        }
    }
}

We have also tried to use the unifiedContacts() API from CNContactStore():

let storeContacts = try self.store.unifiedContacts(
    matching: searchNamePredicate,
    keysToFetch: self.keysToFetch
)
var contacts: [PhoneContactInfo] = []
storeContacts.forEach { contact in
  contacts.append(contentsOf: PhoneContactsFilter.filter(contact: contact, query: query))
}
completion(.success(contacts))
}

We have considered the following factors:

  1. We always request local contact permission whenever the search is needed. searchContacts will be executed after permission is .authorized.
  2. The implementation details of PhoneContactsFilter.filter(contact: contact, query: query) can be considered correct cause it works in most of users.
  3. We tested one of the affected users and found that they were able to search for local contacts with the same code using a different app bundle ID (staging vs production).

We would appreciate any insights on why some users are unable to search for local contacts with the input of the contact's name or number.

Post not yet marked as solved Up vote post of Petergfq Down vote post of Petergfq
318 views