CNContact predicate problem

CNContactPickerViewController can be given a predicate when being configured and the predicate disable selection of the CNContact records that don’t match.

In this case I am trying to filter for any contact that has an iPhone marked as a contact number.

As I understand it, the phoneNumbers array on CNContact has entries that consist of CNLabeledValue objects. A CNLabeledValue has an identifier, label, and value.


The following code works when filtering for a specific identifier.


CNContactPickerViewController *contactPickerViewController = [[CNContactPickerViewController alloc] init];
  [contactPickerViewController setDisplayedPropertyKeys:[NSArray arrayWithObject:CNContactPhoneNumbersKey]];
  NSPredicate *mainPredicate = [NSPredicate predicateWithFormat:@"(phoneNumbers.@count > 0)"];
  NSPredicate *subqueryPredicate = [NSPredicate predicateWithFormat:@"(SUBQUERY(self.phoneNumbers, $contactLabel, $contactLabel.identifier = %@).@count > 0)", @"8550F1E9-F6D1-44EC-99BD-228D82668D57"];
  NSPredicate *predicateContacts = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:mainPredicate, subqueryPredicate, nil]];
  NSPredicate *predicateContactsSelection = [NSPredicate predicateWithFormat:@"(phoneNumbers.@count > 0)"];
  [contactPickerViewController setPredicateForEnablingContact:predicateContacts];
  [contactPickerViewController setPredicateForSelectionOfContact:predicateContactsSelection];
  [contactPickerViewController setDelegate:self];
  [self presentViewController:contactPickerViewController animated:YES completion:nil];


When I change identifier to label, and change the label to the correct value, I only receive errors and all of the contacts are displayed.


[CNUI ERROR] Error when showing picker: Error Domain=CNErrorDomain Code=300 "(null)" UserInfo={CNValidationErrors=(

"Error Domain=CNErrorDomain Code=400 \"Invalid Predicate\" UserInfo={CNKeyPaths=(\n label\n), NSLocalizedDescription=Invalid Predicate, NSLocalizedFailureReason=The operation couldn't be completed because its predicate is invalid.}"

)}



Is it a case that only the identifier can be used in a predicate and not the label?

Already found the issue. My hair almost fell out when I encoutered this problem. The solution is to add apostrophe to value. Like this

let predicate = NSPredicate(format: "ANY self.phoneNumbers.'value'.'digits' BEGINSWITH %@", "+420"

I hope this helps you.

CNContact predicate problem
 
 
Q