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.

Comments

22 Responses to “Access the Address Book”

  1. Using iPhone Address Book - iPhone Dev SDK Forum on September 19th, 2008 3:30 pm

    [...] just wrote up one on my blog Iphone Noob

  2. Retrieving contacts info - iPhone Dev SDK Forum on September 20th, 2008 8:51 am

    [...] iPhone noob just posted an article about this: Iphone Noob

  3. Justin on October 2nd, 2008 5:42 am

    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

  4. Amit on October 8th, 2008 3:10 am

    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?

  5. iSpazio AccessContact - iSpazio Developer Program #7 | iSpazio - Il Blog sull'iPhone più letto in Italia e nel Mondo on October 23rd, 2008 9:11 am

    [...] creata in italiano da Andrea Busi per iSpazio.net. Trovare la guida originale a questo indirizzo: “Access the Address Book: Iphone Noob”. I meriti quindi relativamente alla versione inglese, sono del legittimo [...]

  6. The Bubi Devs » DevTutorial #7 - AccessContact on October 31st, 2008 12:13 pm

    [...] e “The Bubi Devs”. Trovare la guida originale a questo indirizzo: “Access the Address Book: Iphone Noob”. I meriti quindi relativamente alla versione inglese, sono del legittimo [...]

  7. Doug Diego » Blog Archive » iPhone Address Book on November 10th, 2008 8:38 pm
  8. lakshmikanthreddy on January 6th, 2009 2:38 pm

    thank you ,

    thanks a lot , this application has been very much useful for me .

    lakshmi kanth .

  9. kislay sharma on January 19th, 2009 8:50 am

    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

  10. Alex on January 22nd, 2009 4:28 pm

    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

  11. Agustin on February 20th, 2009 3:31 pm

    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.

  12. Yasser on February 24th, 2009 11:25 am

    Hello,
    i can’t find AddressBookUI.framework on the list of FrameWork !!!!

  13. Yasser on February 24th, 2009 12:05 pm

    that’s Work. Thanks.

  14. Tinu on April 10th, 2009 9:27 am

    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!?

  15. S.Lakshmikanth Reddy on May 12th, 2009 5:21 am

    can i get the photos from the Addressbook and display it in my application?

    your help is greatly appreciated.

    Thank you,

    LakshmiKanth Reddy.

  16. Manu on May 28th, 2009 8:08 am

    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???

  17. Santiago Lema on June 15th, 2009 6:52 am

    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);
    }

  18. Prabodha Fonseka on June 18th, 2009 2:19 am

    Thanks a lot. This article was really helpful to me

  19. This was Great on January 8th, 2010 7:46 pm

    How do we save the returned label data collected for use in an email?

  20. iPhone Development SDK – Working with the Address BookRight | de | d.engineering. on January 26th, 2010 2:59 pm

    [...] further information, google around! This is good too http://iphone.zcentric.com/?p=241. I also found the iPhone Developer’s Cookbook Sample Code quite useful. There are a couple [...]

  21. abhi on June 12th, 2010 5:49 am

    lolz. copied word by word from the Apple’s Address Book Programming guide… but thanks for the screenshots anyway. :P

  22. Jayahari on July 31st, 2010 7:00 am

    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….

Leave a Reply