11.4. Retrieving Properties of Address Book Entries

Problem

You have retrieved a reference to an item in the address book, such as a person’s entry, and you want to retrieve that person’s properties, such as first and last names.

Solution

Use the ABRecordCopyValue function on the person’s Address Book record.

Discussion

The records in the address book database are of type ABRecordRef. Each record could be either a group or a person. We have not discussed groups yet, so let’s focus on people. Each person could have various types of information assigned to him, such as his first name, last name, email address, and so on. Bear in mind that many of these values are optional, and at the time of creating a new contact in the address book database, the user can simply leave out fields such as phone number, middle name, email address, URL, and so forth.

ABRecordCopyValue accepts an address book record and the property that has to be retrieved as its two parameters. The second parameter is the property of the record that we want to retrieve. Here are some of the common properties (all of these properties are defined as constant values in the ABPerson.h header file):

kABPersonFirstNameProperty

This value will retrieve the first name of the given person. The return value is of type CFStringRef, which can be cast to NSString with a bridge cast, so you can do just about anything you want with the results.

kABPersonLastNameProperty

This value will retrieve the last name of the given person. Like the first ...

Get iOS 6 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.