0

When I search the ABAddressBook for names of contacts, it returns matches that include job titles.

For example, let's say I've got a contact named John Smith, and he's a Technology Analyst.

If I search for ABAddressBook entries for names that contain[cd] Te, I would like to avoid seeing John's name, since Te is not a substring of "John Smith".

How do I search the ABAddressBook for a name while excluding a job title?

bryanjclark
  • 6,247
  • 2
  • 35
  • 68

1 Answers1

2

Your predicate to filter the array of records should be something like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:
      @"record.name contains[cd] %@", string];

Make sure that record.name only contains the elements identified by

const ABPropertyID kABPersonFirstNameProperty;
const ABPropertyID kABPersonLastNameProperty;
const ABPropertyID kABPersonMiddleNameProperty;

and not

const ABPropertyID kABPersonJobTitleProperty;
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • I know it's been a while but how do you get record from the ABAddressBookCopyArrayOfAllPeople array? All I can get is __NSCFType – John Lane Nov 29 '12 at 15:44
  • Something like `ABRecordRef ref = CFArrayGetValueAtIndex( allPeople, i );` – Mundi Nov 29 '12 at 17:56
  • 1
    Thanks for this. Would you mind posting a snippet? I've tried this: NSMutableArray *records = (__bridge NSMutableArray *)ABAddressBookCopyArrayOfAllPeople( addressBook ); NSPredicate * myPredicate = [NSPredicate predicateWithFormat:@"record.phoneNumber contains %@",@"123"]; try { [records filterUsingPredicate:myPredicate]; } catch (NSException *exception) { NSLog(@"%@",exception); } {} – John Lane Nov 29 '12 at 18:11