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.

About mike
Currently works for OpenSky as a Senior Linux Admin. He has a wonderful wife Thanuja and 2 great dogs. His major side project is Photoblog.

Comments

26 Responses to “Access the Address Book”
  1. Justin says:

    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

  2. Amit says:

    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?

  3. lakshmikanthreddy says:

    thank you ,

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

    lakshmi kanth .

  4. kislay sharma says:

    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

  5. Alex says:

    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

  6. Agustin says:

    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.

  7. Yasser says:

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

  8. Yasser says:

    that’s Work. Thanks.

  9. Tinu says:

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

  10. S.Lakshmikanth Reddy says:

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

    your help is greatly appreciated.

    Thank you,

    LakshmiKanth Reddy.

  11. Manu says:

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

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

  13. Prabodha Fonseka says:

    Thanks a lot. This article was really helpful to me

  14. This was Great says:

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

  15. abhi says:

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

  16. Jayahari says:

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

  17. TTT says:

    Anyone capable of iphone/blackberry apps development please send your cvs to jobsinbahrain.123@gmail.com
    We have an opening for this role

  18. Siddharth says:

    Hey this is good to start with..!!
    Thanx..!!

  19. nafisa says:

    i have do this , but when i clicks at get Name button,the application is crshed.
    can anybody guid me

Trackbacks

Check out what others are saying about this post...
  1. [...] just wrote up one on my blog Iphone Noob

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

  3. [...] 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 [...]

  4. [...] 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 [...]

  5. [...] 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 [...]

  6. [...] da Andrea Busi per “devAPP”. Trovare la guida originale a questo indirizzo: “Access the Address Book: Iphone Noob“. I meriti delle rispettive versioni, quindi, sono dei legittimi autori. Categorie: Tutorial [...]



Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!