Is it possible to fetch contacts from CNContactStore using custom predicate?

I've been working with swift and swiftui for about a year (but I'm old so not so swift (sorry)). I am trying to construct a predicate other than those supplied in the framework (i.e., name, email, phone) to fetch contacts having a specific value in their socialProfiles. For example, I want to fetch all contacts having a urlString that contains a specific value.

I've finding the documentation on NSPredicate to be a bit daunting, especially since I have never used Objective-C, only swift. I think the problem is I don't know how to refer to the socialProfiles value in the predicate format string, nor what operators I should be using to filter.

I'm sure the code below is totally wrong, and I'm not sure it can even be done, but to help illustrate what I'm trying to do:

keys = [CNContactSocialProfilesKey] as [CNKeyDescriptor] predicate = NSPredicate(format: "SELF.socialProfile.value.urlString contains %@", value) do { let contacts = try store.unifiedContacts(matching: predicate, keysToFetch: keys) return contacts }

Any suggestions would be appreciated, including: "You can't do that...you've got to retrieve all the contacts and filter them yourself"

  • While digging around on StackOverflow, I found a posting that says specifically you can't use any predicates besides those supplied. Unless anyone has another point of view, I think that probably ends the discussion.

Add a Comment