views:

218

answers:

1

Is there a way to find a particular kind of email address for a person from the iPhone Address Book? I know how to get all of the email addresses for a person, just not how to identify what kind of e-mail address it is ("home", "work", etc.)...nor (and this might be preferable), a way to directly access that address without having to iterate through them all.

Thanks.

+1  A: 

Check for a label of kABWorkLabel using ABMultiValueCopyLabelAtIndex.

For example, if you have an ABRecordRef named "person", this code will set a single NSString named "emailAddress":

// Email address (if only one, use it; otherwise, use the first work email address)
CFStringRef value, label;
ABMutableMultiValueRef multi = ABRecordCopyValue(person, kABPersonEmailProperty);
CFIndex count = ABMultiValueGetCount(multi);
if (count == 1) {
    value = ABMultiValueCopyValueAtIndex(multi, 0);
    emailAddress = (NSString*)value;
    [emailAddress retain];
    CFRelease(value);
} else {
    for (CFIndex i = 0; i < count; i++) {
        label = ABMultiValueCopyLabelAtIndex(multi, i);
        value = ABMultiValueCopyValueAtIndex(multi, i);

        // check for Work e-mail label
        if (CFStringCompare(label, kABWorkLabel, 0) == 0) {
            emailAddress = (NSString*)value;
            [emailAddress retain];
            break;
        }

        CFRelease(label);
        CFRelease(value);
    }
}
CFRelease(multi);
gerry3
Just to condense the response, the secret here is the kABWorkLabel which gets out a work address. Note that your code has a potential leak issue though, as there could be a number of work email addresses so emailAddress should either be released before setting, or you need to break out of the loop, or (better) you need to spit out an array of work email addresses.
Kendall Helmstetter Gelner
Good points. I took this from my app and I was actually setting a property (no leak) and I only care about geting one.
gerry3