Cover by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

10.7. Searching the Address Book

Problem

You want to find a specific person or group in the address book database.

Solution

Use the ABAddressBookCopyArrayOfAllPeople and ABAddressBookCopyArrayOfAllGroups functions to find all people and groups in the address book. Traverse the returned arrays to find the information you are looking for. Alternatively, you can use the ABAddressBookCopyPeopleWithName function to find an entry about a person with a specific name.

Discussion

Up to this point, we have been inserting group and person entries into the address book without checking whether such a group or person already exists. We can use the ABAddressBookCopyArrayOfAllPeople and ABAddressBookCopyArrayOfAllGroups functions to get the array of all people and groups in the address book and search in the array to see whether the person or group entries we are about to insert into the address book already exist. When we check whether strings match, we also have to check for null strings (which we assume mean that the contacts match). Here are two methods that will make use of these functions and that can also be used in other recipes:

- (BOOL) doesPersonExistWithFirstName:(NSString *)paramFirstName lastName:(NSString *)paramLastName inAddressBook:(ABRecordRef)paramAddressBook{ BOOL result = NO; if (paramAddressBook == NULL){ NSLog(@"The address book is null."); return NO; } NSArray *allPeople = (__bridge_transfer NSArray *) ABAddressBookCopyArrayOfAllPeople(paramAddressBook); NSUInteger peopleCounter ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required