Lately I’ve spent a bit of time learning the API for the iPhone Contacts Address Book. I’d like to share the process and help with some of the bits I found difficult.

Including the Frameworks

First the framework must be included in your Xcode project.

  1. Right Click on your project target and in the linked  libraries add AddressBook.framework, and AddressBookUI.framework
  2. To add these, click the + button at the bottom of the properties box. In my case, the AddressbookUI.framework was not in the list. To add it I clicked ‘Add Other’ and navigated to /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.1.sdk/System/Library/Frameworks. AddressBookUI.framework is in here. (if you don’t have folder 2.2.1, try any of the others).
  3. If done correctly, your project should look like the one below.

    Don’t be alarmed if your frameworks are all red in your Xcode project, this is in fact how you want it.

Class Include

To access the address book, in the working class header insert the following:

#import
//AddressBook stuff
#import
#import

Displaying Contact List

The first thing I wanted to do was to draw the list of iPhone contacts, to allow one to be selected, and used as input.

On a click of an icon I execute the method: getContact. (See below). This calls the contact list to display, but it also requires the below peoplePickerNavigationControllerDidCancel,peoplePickerNavigationController and peoplePickerNavigationController.

What the following code does is:

1. On click of getContact (figure 1), shows the contact list (figure 2), on selecting a contact from the list the contacts name (and company if available) is put into the textfields (figure 3). Simple right?


getContact

-(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];
}

peoplePickerNavigationController

- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
NSString *contactName;
NSString *contactCompany;
NSString *contactFirst;
NSString *contactLast;
contactFirst = [(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) stringByAppendingString:@" "];
contactLast =  (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
contactName = [contactFirst stringByAppendingString:contactLast];
contactCompany = (NSString *)ABRecordCopyValue(person, kABPersonOrganizationProperty);
NSNumber *recordId = [NSNumber numberWithInteger: ABRecordGetRecordID(person)];
NSLog(@"record id is %@",recordId);
NSLog(@"Person Reference: %d", person);
NSLog(@"Name: %@", contactName);
NSLog(@"Company: %@", contactCompany);
txtName.text = contactName;
NSString *personStr = [NSString stringWithFormat:@"%@", recordId];
txtPersonID.text = personStr;
if (contactCompany != nil) txtCompanyName.text = contactCompany;
// remove the controller
[self dismissModalViewControllerAnimated:YES];
return NO;
}

peoplePickerNavigationControllerDidCancel

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

peoplePickerNavigationController

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

Not only do I use the name of the contact, but I also access the vital PersonRecordID, which is more or less a unique identifier for each contact. For more information on this, search on the topic ABAddressBookGetPersonWithRecordID.

Could you follow that? Its pretty interesting to implement. There is of course the ability to access all the data in a contact, make edits, add new contacts, but I wanted to share what I have done myself.

For 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 good projects in Chapter 9 – People, Places, Things.

References