Datasheet

780
Interfacing with the Address Book Application
ABMultiValueRef multiValueRef = (NSString*)
ABRecordCopyValue(person, kABPersonPhoneProperty);
In this case, the property you’re getting is the Phone property (kABPerson-
PhoneProperty) and is a kABMultiStringPropertyType (think string).
Because there can be zero or many phone numbers, you get the count and
enumerate through the record.
for(int i=0; i < ABMultiValueGetCount(multiValueRef);
i++) {
For each entry in the record, you check to see whether it has the label of the
number you’re interested in, and save it if it does.
if([phoneLabel isEqualToString:
(NSString*)kABPersonPhoneIPhoneLabel])
iPhone = (NSString*)
ABMultiValueCopyValueAtIndex(multiValueRef,i);
if([phoneLabel isEqualToString:(NSString*)kABHomeLabel])
homePhone = (NSString*)
ABMultiValueCopyValueAtIndex(multiValueRef,i);
The first phone type — kABPersonPhoneIPhoneLabel — is listed under
Phone Number Property in the ABPerson Reference, along with a bunch of
others. The kABHomeLabel is under Generic Property Labels.
As I said, what makes it interesting is that there are really two kinds of mulit-
value labels. The first (phone number) was a kABMultiStringProperty-
Type. Street address however is a kABMultiDictionaryPropertyType.
Although street address is still an ABMultiValueRef property, it isn’t a
kABMultiStringPropertyType — it’s, as I said, kABMultiDictionary-
PropertyType instead. As such, it is a dictionary entry, which means you’ll
have to first get the dictionary and then get the values you’re interested in.
Street addresses are represented as a multi-value of dictionaries. Each value
has a label, such as home or work. Within the value, the dictionary contains
keys for the different parts of a street address.
In the following section of the code you entered as part of Listing 1-20, you
simply check to see whether there’s an entry, and if so, you take the first
street address.
if (ABMultiValueGetCount(multiValueRef) > 0) {
CFDictionaryRef dictionary = ABMultiValueCopyValueAtIndex
(multiValueRef, 0);
street.text = (NSString*) CFDictionaryGetValue
(dictionary, kABPersonAddressStreetKey);
41_542934-bk07ch01.indd 78041_542934-bk07ch01.indd 780 3/23/10 11:01 PM3/23/10 11:01 PM