Enable phoneNumbers of certain format using predicateForEnablingContact in CNContactPickerViewController

148 Views Asked by At

I'm looking to use predicateForEnablingContact inside a CNContactPickerViewController to only enable contacts that have phoneNumbers that match the following regular expression ^\\+1[2-9]\\d{9}$.

This will mean looking at a contacts phoneNumbers and checking to see if at least one or more matches the following expression.

I started small by using the following trying to following the NSPredicate docs here:

let predicate = NSPredicate(format: "ANY self.phoneNumbers.'value'.'stringValue' BEGINSWITH %@", "1111")
contactPicker.predicateForEnablingContact = predicate

However, I'm unable to get past this part. I always seem to get the following error in my logs when running:

2023-06-11 21:31:15.703957+0100 Test2[9610:702640] [CNUI ERROR] Error when showing picker: Error Domain=CNErrorDomain Code=300 "(null)" UserInfo={CNValidationErrors=(
"Error Domain=CNErrorDomain Code=400 \"Invalid Predicate\" UserInfo={NSLocalizedDescription=Invalid Predicate, NSLocalizedFailureReason=The operation couldn\U2019t be completed because its predicate is invalid.}"

Does anyone have any ideas as to how I'd write a NSPredicate that matches the ruleset above please?

Edit:

I have also tried using

NSPredicate(format: "phoneNumbers.@count > 0 AND SUBQUERY(phoneNumbers, $phoneNumber, $phoneNumber.value.stringValue != nil).@count > 0"` 

But get the following...

predicate is invalid., CNKeyPaths=(\n \"value.stringValue\"\n)}"
0

There are 0 best solutions below