11.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
*
)
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 ...
Get iOS 6 Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.