13.3. Retrieving All the People in the Address Book
Problem
You want to retrieve all the contacts in the user’s address book.
Solution
Use the ABAddressBookCopyArrayOfAllPeople function to
retrieve an array of all contacts:
-(void)readFromAddressBook:(ABAddressBookRef)paramAddressBook{NSArray*arrayOfAllPeople=(__bridge_transferNSArray*)ABAddressBookCopyArrayOfAllPeople(paramAddressBook);NSUIntegerpeopleCounter=0;for(peopleCounter=0;peopleCounter<[arrayOfAllPeoplecount];peopleCounter++){ABRecordRefthisPerson=(__bridgeABRecordRef)[arrayOfAllPeopleobjectAtIndex:peopleCounter];NSLog(@"%@",thisPerson);/* Use the [thisPerson] address book record */}}
Discussion
After accessing the user’s address book database, we can
call the ABAddressBookCopyArrayOfAllPeople function to retrieve an array
of all the contacts in that address book. The return value of this function is an immutable array of
type CFArrayRef. You can’t work with
this type of array as you would work with instances of NSArray, but you have two ways to traverse a
CFArrayRef array. First, it natively
supports two functions:
CFArrayGetCountGets the number of items in an instance of
CFArrayRef. This is similar to thecountinstance method of anNSArray.CFArrayGetValueAtIndexRetrieves an item at a specific location of an instance of
CFArrayRef. This is similar to theobjectAtIndex:instance method of anNSArray.
Second, the CFArrayRef Core Foundation object is one of the objects that supports toll-free bridging ...
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