ABPerson: How to get person's home and work phones (not the FAX ones)

1.6k views Asked by At

I want to know if it's possible to extract a contact's Home Phone Number and Work Phone Number, instead of their Home FAX or Work FAX. If not, why is this a restriction?

The reference only mentions the following constants:

const ABPropertyID kABPersonPhoneProperty;  
const CFStringRef kABPersonPhoneMobileLabel;  
const CFStringRef kABPersonPhoneIPhoneLabel;  
const CFStringRef kABPersonPhoneMainLabel;  
const CFStringRef kABPersonPhoneHomeFAXLabel;  
const CFStringRef kABPersonPhoneWorkFAXLabel;  
const CFStringRef kABPersonPhoneOtherFAXLabel;  
const CFStringRef kABPersonPhonePagerLabel;

But if you use your iPhone, you'll notice there's many more labels than that (not to mention the custom ones). How can I pick them?

2

There are 2 answers

2
Grzegorz Krukowski On
//contactData is ABRecordRef
ABMultiValueRef phones = ABRecordCopyValue(contactData, kABPersonPhoneProperty);

for (CFIndex i=0; i < ABMultiValueGetCount(phones); i++) 
{
    NSString* phoneLabel = (NSString*) ABMultiValueCopyLabelAtIndex(phones, i);
    NSString* phoneNumber = (NSString*) ABMultiValueCopyValueAtIndex(phones, i);

    //for example
    if([phoneLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
    {
        //under phoneNumber you have a kABPersonPhoneMobileLabel value
    }
    .. add other standard labels
    else //custom label
    {

    }

    [phoneNumber release];
    [phoneLabel release];
}

CFRelease(phones);
0
Jingshao Chen On

kABHomeLabel and kABWorkLabel

if (CFStringCompare(phoneLabelRef, kABHomeLabel, 0) == kCFCompareEqualTo) {
        homePhone = (__bridge NSString *)phoneNumberRef;
} else if (CFStringCompare(phoneLabelRef, kABWorkLabel, 0) == kCFCompareEqualTo) {
        officePhone = (__bridge NSString *)phoneNumberRef;
}

See this excellent tutorial: http://www.appcoda.com/ios-programming-import-contact-address-book/