For a project I am currently working on, I had to retrieve all the emails from the user's iPhone address book. The purpose was to later check against the web service database and find whether some of the user's friends already were using the app.

Diving into the AddressBook framework is pretty trivial but since it is all C-based, it can feel unfamiliar to coder mainly used to the Objective-C syntax.

Following is the code I wrote in order to achieve the previously described. Note that this code does not check for errors throughout the execution. It is only written in order to roughly describe the method but some checks would definitely have to be added.

// we will finally store the emails in an array so we create it here
NSMutableArray *emails = [NSMutableArray array] ;

// get the address book
ABAddressBookRef addressBook = ABAddressBookCreate() ;

// get an array of all the persons (records) in this address book
CFArrayRef addressBookRecords = ABAddressBookCopyArrayOfAllPeople(addressBook) ;

// loop through the address book records
for (int i = 0 ; i < CFArrayGetCount(addressBookRecords) ; i++)
{
	// get a single person as a record
	ABRecordRef person = CFArrayGetValueAtIndex(addressBookPersons, i) ;
	// get all email associated with this person
	ABMultiValueRef emailProperty = ABRecordCopyValue(person, kABPersonEmailProperty) ;
	// convert it to an array
	CFArrayRef allEmails = ABMultiValueCopyArrayOfAllValues(emailProperty) ;
	// add these emails to our initial array
	[emails addObjectsFromArray: (NSArray *)allEmails] ;
	// release memory
	CFRelease(allEmails) ;
	CFRelease(emailProperty) ;
}

// release memory
CFRelease(addressBook) ;
CFRelease(addressBookRecords) ;

// print the elements in the array to check everything worked fine
for (NSString *email in emails)
{
	NSLog(@"%@", email) ;
}

Also, make sure you import the Address Book framework by inserting the following line at the top of your header. You will also need to manually add the framework to your project.

#import <AddressBook/AddressBook.h>