views:

184

answers:

1

When I try to retrieve phone numbers from Address Book contacts, the result is always nil if the contact has an e-mail address. This is obviously a big problem...such a big one, in fact, that I have a hard time believing I'm not doing something wrong. But every test I've done--including using other, unrelated code samples--gives the same result.

I've used the code below to successfully retrieve email addresses:

+ (NSArray*)emailFromRecordRef:(ABRecordRef)personRef
{
    ABMultiValueRef emailAddressesRef = ABRecordCopyValue(personRef, kABPersonEmailProperty);
    NSArray* emailAddresses = nil;
    if (emailAddressesRef != nil)
    {
        emailAddresses = [(NSArray*)ABMultiValueCopyArrayOfAllValues(emailAddressesRef) autorelease];
        CFRelease(emailAddressesRef);       
    }

    return emailAddresses;
}

...but when I adapt the same code to retrieve phone numbers:

+ (NSArray*)phoneNumbersFromRecordRef:(ABRecordRef)personRef
{
    ABMultiValueRef phoneNumbersRef = ABRecordCopyValue(personRef, kABPersonPhoneProperty);
    NSArray* phoneNumbers = nil;
    if (phoneNumbersRef != nil)
    {
        phoneNumbers = [(NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumbersRef) autorelease];
        CFRelease(phoneNumbersRef);     
    }

    return phoneNumbers;
}

...it only retrieves phone numbers for users that don't have email addresses. If they have email addresses, no phone numbers are returned.

Has anyone out there successfully retrieved phone numbers from Address Book contacts?

Thanks.

+1  A: 

Turns out, of course, I was doing something wrong. Apparently you need to keep the reference to AddressBook alive (retained) during the entire time you're requesting additional attributes like phone numbers. When I kept it alive, the phone number issue went away.

Greg Maletic