Datasheet

778
Interfacing with the Address Book Application
As one of the arguments of peoplePickerNavigationController:
shouldContinueAfterSelectingPerson:, you are passed the record of
the person selected:
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
As I mentioned, inside the person record are properties, and there are two
kinds: single-value properties and multi-value properties.
Single-value properties are properties that a person can have only one of,
such as first name and last name. (Okay, maybe I should say you only have
one legal first and last name.) You’ll start things off by taking care of your
single-value properties, as follows:
name.text = (NSString*) ABRecordCopyValue
(person, kABPersonOrganizationProperty);
When you’re passed a person record, the way you access the single-
value property — organization or name, for example — is through the
ABRecordCopyValue function, which returns the value of a record prop-
erty — in this case, kABPersonOrganizationProperty — as a string.
It actually returns a CFTypeRef, which is an untyped generic reference to
any Core Foundation object. You cast it in the ABRecordCopyValue func-
tion to avoid compiler warnings.
I know this syntax may look weird to you, but that’s because this is not
iPhone specific. It comes from Core Foundation (on the Mac) which is a set
of C-based programming interfaces that implement simple object models in
C that encapsulate data and functions as system-managed objects and oper-
ate seamlessly with Cocoa Foundation interfaces.
kABPersonOrganizationProperty, and kABPersonFirstNameProp-
erty, and kABPersonLastNameProperty are constants defined by Apple
that specify which fields you’re accessing. They’re listed in the XCode
documentation under Personal Information Properties in the ABPerson
Reference document.
Here I have to make a few decisions. Hotels in my Address Book will have
the name of the hotel in the kABPersonOrganizationProperty, and I’ll
use that for the name in my view display and the hotel object. But for my
friend’s first cousin’s ex-boyfriend, there won’t be one, so I’ll take the first
and last name properties, concatenate them, and display it as the hotel
name instead. (I’ll leave it to you to figure out what to do if the joker finally
did find a job and his new company name is in his contact information.)
41_542934-bk07ch01.indd 77841_542934-bk07ch01.indd 778 3/23/10 11:01 PM3/23/10 11:01 PM