Here we are going to show you how to print the first and last names for all records in the iPhone Address Book to the output console. First you need to include the following import statement:

#import <AddressBook/AddressBook.h>

Also, add the AddressBook.framework to your project.

To display all the address book names, create an address book reference, copy all records into an array and now for each entry, access the first and last name properties.

ABAddressBookRef addressBook = ABAddressBookCreate();
 
// Can we find the address book...
if (addressBook != nil)
{
// Get an array address book entries
CFArrayRef arrayOfEntries = ABAddressBookCopyArrayOfAllPeople(addressBook);
 
// Are there entries in the array?
if (CFArrayGetCount(arrayOfEntries) > 0)
{
CFIndex countOfEntries = CFArrayGetCount(arrayOfEntries);
NSLog(@"Address book entries: %ld", countOfEntries);
 
for (int x = 0; x < countOfEntries ; x++)
{
// Get the current record
ABRecordRef activeRecord = CFArrayGetValueAtIndex(arrayOfEntries, x);
 
// Print first name
NSString *firstname = (NSString *) ABRecordCopyValue(activeRecord, kABPersonFirstNameProperty);
NSLog(@"First name: %@", firstname);
[firstname release];
 
// Print last name
NSString *lastname = (NSString *) ABRecordCopyValue(activeRecord, kABPersonLastNameProperty);
NSLog(@"Last name: %@", lastname);
[lastname release];
}
}
 
CFRelease(arrayOfEntries);
CFRelease(addressBook);
}

And this will show first and last names of all records in address book. Hope you like it...

I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too. 

Stay tuned to my blogtwitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.