iPhone Tutorial for Retrieving Contact information from AddressBook

Introduction

In this tutorial I will explain how you can retrieve a Contact(s) from your AddressBook (in iPhone) though coding. In iPhone you can retrieve contact information stored in address book easier then you can think. You can retrieve their pictures (photos), email address(es) and anything user stored in AddressBook. Note: iPhone provided easy access to user contacts but put a good restriction on editing those. Your application can retrieved all contact information but could not change it. Your application can edit it and present it to user, user click on save then those changes will be stored in AddressBook. In this tutorial, I will explain simple steps to retrieve user contact Information and final output will look like this:

Picture 13 iPhone Tutorial for Retrieving Contact information from AddressBook
Fig 11. Final output

Request Full Filled

This tutorial is for Sudha who requested for this in ‘Request iPhone Tutorial‘ section (Note: any one can request for iPhone tutorial there).

Get Started

Now, start following the steps to retrieve contact information from AddressBook.
Step 1. Create a new Xcode project, name it ‘RetrieveContactInfo’

Picture 1 iPhone Tutorial for Retrieving Contact information from AddressBook
Fig 1. Create Xcode Project
Picture 2 iPhone Tutorial for Retrieving Contact information from AddressBook
Fig 2. Give name to your Xcode Project

Step 2. In ‘Group & File’ panel, expand ‘Frameworks’ section. Right click on any framework and then select ‘Reveal in Finder’ (figure 3). In new finder window scroll to top and you will see two frameworks there ‘AddressBook.framework’ and ‘AddressBookUI.framework’. Drag them inside your ‘Frameworks’ folder & do not click on ‘Copy items into destination group’s folder’.
[5,6,7,8,9,10]

Picture 5 iPhone Tutorial for Retrieving Contact information from AddressBook
Fig 3. Add framework to your application
Picture 6 iPhone Tutorial for Retrieving Contact information from AddressBook
Fig 4. Add framework to your application
Picture 7 iPhone Tutorial for Retrieving Contact information from AddressBook
Fig 5. Add framework to your application
Picture 8 iPhone Tutorial for Retrieving Contact information from AddressBook
Fig 6. Drag the AddressBook.framekwork and AddressBookUI.framework
Picture 9 iPhone Tutorial for Retrieving Contact information from AddressBook
Fig 7. Do not select copy checkbox
Picture 10 iPhone Tutorial for Retrieving Contact information from AddressBook
Fig 8. Added framework view.

3. Remove all the code inside ‘RetrieveContactInfoViewController.h’ and place the following code:

//
//  RetrieveContactInfoViewController.h
//  RetrieveContactInfo
//
//  Created by Adeem on 26/05/09.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//

#import
#import
#import
@interface RetrieveContactInfoViewController : UIViewController  {
ABPeoplePickerNavigationController *picker;
IBOutlet UILabel *phoneNo;
IBOutlet UILabel *email;
IBOutlet UILabel *name;
}

-(IBAction)chooseContacts;

@end

4. Now in ‘RetrieveContactInfoViewController.m’ class before @end, write the following code:

-(IBAction)chooseContacts {
// creating the picker
if(!picker){
picker = [[ABPeoplePickerNavigationController alloc] init];
// place the delegate of the picker to the controll
picker.peoplePickerDelegate = self;
}
// showing the picker
[self presentModalViewController:picker animated:YES];
}

/**************  Contacts Delegate Functions  ***************/

- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSMutableArray *phones = [[NSMutableArray alloc] init];
int i;
for (i = 0; i < ABMultiValueGetCount(phoneMulti); i++) {
NSString *aPhone = [(NSString*)ABMultiValueCopyValueAtIndex(phoneMulti, i) autorelease];
//	NSLog(@"PhoneLabel : %@ &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp; Phone# : %@",aLabel,aPhone);
[phones addObject:aPhone];
}

NSString *mobileNo = [phones objectAtIndex:0];
phoneNo.text = mobileNo;
NSLog(mobileNo);
name.text = (NSString*)ABRecordCopyCompositeName(person);

ABMutableMultiValueRef emailMulti = ABRecordCopyValue(person, kABPersonEmailProperty);
NSMutableArray *emails = [[NSMutableArray alloc] init];
for (i = 0; i  0)
{
NSString *emailAdress = [emails objectAtIndex:0];

email.text = emailAdress;
NSLog(emailAdress);
}
[peoplePicker dismissModalViewControllerAnimated:YES];

return YES;
}

- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier{

return NO;

}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
// assigning control back to the main controller
[picker dismissModalViewControllerAnimated:YES];
}

5. Open ‘RetrieveContactInfoViewController.xib’ file which you can find in ‘Group & File’ panel. Place three labels and one button inside view.

Picture 11 iPhone Tutorial for Retrieving Contact information from AddressBook
Fig 9. Template design for your view

6. Select ‘Files Owner’ in xib file and press cmd + 2 to open ‘Connection Inspector’. In Outlets, click on circle next to email and drag it to second label. Same way map all the labels with controller.

Picture 12 iPhone Tutorial for Retrieving Contact information from AddressBook
Fig 10. Map class variables with Interface Builder

7. Run the application and final output will be like this

Picture 13 iPhone Tutorial for Retrieving Contact information from AddressBook
Fig 11. Final output

You can grab the code from here.

This is a guest post by Zubair Daud, an iPhone developer. Click here to find out more about him.

Popularity: 41% [?]

13 Comments on "iPhone Tutorial for Retrieving Contact information from AddressBook"

  1. Sreelatha says:

    Hi Zubair,

    Nice post from you.

    I am trying to edit a selected contact using iPhone programming. I have followed AddressBook Programming guide. The selected contact does not open up in the Edit mode. I am trying very hard for working this but not able to succeed.

    Can you please provide some post related to editing a selected contact. It would be a great help for me pls.

    Thanks & Regards,
    Sreelatha.

  2. Kit says:

    I had trouble with the code as posted on this page for retrieving an email address from the contacts list. The missing code was complete in the downloadable code: http://adeem.me/code/RetrieveContactInfo.zip

  3. Kit says:

    I had trouble with the code as posted on this page for retrieving an email address from the contacts list. The missing code was complete in the downloadable code:
    link

  4. Adeem Basraa says:

    Thax kit, I will double check it and see what i missed in the post.

  5. Sreelatha says:

    Hi Zubair,

    Can you please let me know how can I edit a contact using iPhone programming.
    I had followed “AddressBookProgramming” Guide but it does not work for me . pls let me know how can I do it.

    Thanks&Regards,
    Sreelatha.

  6. vyoumans says:

    I have not started this tutorial yet, but I have reviewed it.
    Q1: you say in your first paragraph:
    “Note: iPhone provided easy access to user contacts but put a good restriction on editing those. Your application can retrieved all contact information but could not change it. Your application can edit it and present it to user, user click on save then those changes will be stored in AddressBook.”

    I am confussed… the first line says that you can NOT save changes to a contact in AB. But in the End of the quote, you say that the User can save the changes. could you explain more about what you are talking about here please.

    I am looking for a way to make changes to the AB. I want to change phone numbers.

  7. vyoumans says:

    Question 2: in AB, there are 4 main phone numbers: work, home, main, mobile. WHat if I wanted to add a special number category – “international01″, “international02″…
    this would mean that i Have to add the categories, then update the field.

    Question 3: in the Address Book application, there is a way to add groups, then place contacts in those groups. But you can only do this if your are running a MAC. If you are running WIndows, then you dont have this ability. So, I would like to add a capability to add a group from my iPhone APP programatically.
    then add contacts to it. Doing this will allow me to keep a consistent user experience across MACs and PC’s.

    thanks

  8. Digital Duane says:

    Thanks for the great tut! I’d been trying for days to find working code for accessing Address Book info.
    How can that retrieved contact info be interactive so that the user can click(touch) the phone number and the call is placed or touch the email data line and an email shared app is activated? That would make this an incredible application. Thanks again.

    Digital Duane

  9. Digital Duane says:

    INCREDIBLE! I found the tutorial on how to dial a number, send email, and send an sms message. You are awesome! Thanks Again.

    Digital Duane

  10. Digital Duane says:

    Your donation app doesn’t have a US Dollar option. Or am I missing it?

    Digital D

  11. jissa says:

    Thanks for the great tutorial .
    is there any code to retrieving mail body from iPhone mail box?
    if yes please send me some code.

  12. jayant says:

    thanks for this useful info.

    can u please tell me how to retrieve all contacts and its info.
    i don’t want to select a contact. through application i want to retrieve all contacts and its info.

    if possible please send me some sample code.

Trackbacks for this post

  1. iPhoneGeek 爱疯极客 » 从地址簿提取联系人信息

Got something to say? Go for it!