Access the Address Book
I got a request on how do I access the address book in the iPhone. So I took around 30 minutes to learn how and here is how we will do it.
First create a new View-Based Application and call it addressBook.
Now the first thing we want to do is setup the 4 IBOutlets and the 1 IBAction for our project. We also need to include the headers for the address book framework. So open up addressBookViewController.h and you want to make it look like the following.
#import <UIKit/UIKit.h> #import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h> @interface addressBookViewController : UIViewController { IBOutlet UIButton *button; IBOutlet UILabel *firstName; IBOutlet UILabel *lastName; IBOutlet UILabel *number; } -(IBAction)getContact; @end
Now that they are added we need to add in the framework files. So you will right click on the addressBook in the Targets option on the left panel and click on Get Info.
At the bottom there is a + and click that and you need to add the following items in AddressBook.framework and AddressBookUI.framework
Now lets layout our view. So double click on addressBookViewController.xib. You want your view to look like the following.
I used the —–’s just so you can see there is a UILabel there. Now we need to setup the connections for the 4 outlets and the one action. You want it to look like the following

So now lets open up addressBookViewController.m and we want to add in the following methods.
-(IBAction)getContact {
// creating the picker
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
// place the delegate of the picker to the controll
picker.peoplePickerDelegate = self;
// showing the picker
[self presentModalViewController:picker animated:YES];
// releasing
[picker release];
}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
// assigning control back to the main controller
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
// setting the first name
firstName.text = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
// setting the last name
lastName.text = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
// setting the number
/*
this function will set the first number it finds
if you do not set a number for a contact it will probably
crash
*/
ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
number.text = (NSString*)ABMultiValueCopyValueAtIndex(multi, 0);
// remove the controller
[self dismissModalViewControllerAnimated:YES];
return NO;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
return NO;
}
Now we can click build and go and see the following

As always you can grab the source here.






Did you just pull this straight from the AddressBook Programming Guide? You really should reference your sources
Have you had any success modifying the people-picker at all to get it to link up with the navigation controller from the rest of an application?
/J
Is it possible to replace or extend the default phone/contact application in iPhone? I will be building an new contact application and I dont to override the default one so that when a user clicks the iPhone default icon for contacts (or phone) then it either invokes my application or gives me some way to extend its functionality?
Is it really possible?
thank you ,
thanks a lot , this application has been very much useful for me .
lakshmi kanth .
can any one tell me how to create a new contact detail from coding itself like we can fetch the data from contact list So how to save from application to contact list
Hi Everybody, I try to use the iPhone Calendar from my ipa. Therefore, could you please let me know the correct way to implement the iCal interface? Is this possible?
Thanks a lot for any help about it,
Alex
Hi There,
Is there a way to access to country codes from the iphone contacts to integrated to my application. Or do I have to create my own contries table?
Thanks.
Hello,
i can’t find AddressBookUI.framework on the list of FrameWork !!!!
that’s Work. Thanks.
Mine crashes after clicking the button… and I found out because of “[self presentModalViewController:picker animated:YES];”. But your project works fine.
Is this because my app is window based, and your’s view based!?
can i get the photos from the Addressbook and display it in my application?
your help is greatly appreciated.
Thank you,
LakshmiKanth Reddy.
Has anyone implemented something to prevent crashing if the contact does not have phone number?.. and is it possible to show all the numbers that ONE people has? like main, house, mobile???
To prevent crashing if there’s no value just check the number of values:
ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
if (ABMultiValueGetCount(multi)>0)
{
NSString* nb = (NSString*)ABMultiValueCopyValueAtIndex(multi, 0);
NSLog(@”Picked Number:%@”,nb);
}
Thanks a lot. This article was really helpful to me
How do we save the returned label data collected for use in an email?
lolz. copied word by word from the Apple’s Address Book Programming guide… but thanks for the screenshots anyway.
hi,
I need to filter the AddressBook with the given string. Can anyone knows it???
I tried to access the searchBar on the pickerView. but i cant get access to it.
Then i tried to create a addressbook with only matching person records with ABAddressBookCopyPeopleWithName keyword..
Please help me, thanks in advance….
Anyone capable of iphone/blackberry apps development please send your cvs to jobsinbahrain.123@gmail.com
We have an opening for this role
Hey this is good to start with..!!
Thanx..!!
i have do this , but when i clicks at get Name button,the application is crshed.
can anybody guid me