13.8. 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 AB
AddressBookCopyArrayOfAllPeople 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*)paramFirstNamelastName:(NSString*)paramLastNameinAddressBook:(ABRecordRef)paramAddressBook{BOOLresult=NO;if(paramAddressBook==NULL){NSLog(@"The address book is null.");returnNO;}NSArray*allPeople=(__bridge_transferNSArray*)ABAddressBookCopyArrayOfAllPeople(paramAddressBook);NSUIntegerpeopleCounter ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access