Reputation: 415
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.
Upvotes: 1
Views: 4281
Reputation: 145
For anyone who sees this post. In iOS11, Apple introduced this new method to fetch contact with phone number. https://developer.apple.com/documentation/contacts/cncontact/3020511-predicateforcontacts
See: https://nshipster.com/ios-12/
Upvotes: 6
Reputation: 316
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
Upvotes: 2