iosswiftnspredicatecncontactcncontactstore

phone number predicate on CNContactStore


I am building an app in Swift. I would like to make predicate using a phoneNumber (as String) and retrieve the name of that contact -if it exists- from the CNContactStore. I can make a name predicate fairly easily by :

let pred = CNContact.predicateForContactsMatchingName(name: String)

But is there a way to do the same for a PhoneNumber. I can of course fetch the entire CNContactStore, loop through it and retrieve the contact. But I was wondering if there was any better way.


Solution

  • In short: you can't create a predicate to filter based on phone number. You have to pull all of the contacts and iterate. NOTE: For any given phone number, it 1) may not exist in contacts, or 2) exist more than once.

    Check out this post: https://forums.developer.apple.com/thread/19329