Manlio
Manlio

Reputation: 10865

Cannot read kABPersonPhoneProperty

I'm facing a (strange) problem: I'd like to retrieve the number of phone's numbers of a contact but, for some reason, I am not able to.

I used

ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *contacts = (NSArray*) ABAddressBookCopyArrayOfAllPeople(addressBook);
CFRelease(addressBook);

to get the array of all the contacts. Then I would like to use

ABMultiValueRef ref = ABRecordCopyValue([contacts objectAtIndex:i], kABPersonPhoneProperty);

NSLog(@"%d",ref==NULL);

but ABRecordCopyValue always returns nil...
Notice that I'm able to retrieve other informations about the contact: for example, extracting the name works fine using

CFStringRef name = ABRecordCopyCompositeName([contacts objectAtIndex:i]);


May someone explain me what I'm doing wrong? I'm using Snow Leopard with Xcode 4.2 and I'm developing for iOS 4.0...

EDIT: I found a solution: instead of using

ABRecordCopyValue([contacts objectAtIndex:i], kABPersonPhoneProperty);

I used

ABRecordID idRec = ABRecordGetRecordID([contacts objectAtIndex:i]);
ABMultiValueRef ref = ABRecordCopyValue(ABAddressBookGetPersonWithRecordID(addressBook, idRec), kABPersonPhoneProperty);

However I had to keep valid the reference to addressBook (do not release it), thus the solution suggested by EricS seems better.

Upvotes: 3

Views: 2444

Answers (1)

EricS
EricS

Reputation: 9768

This is just a guess, but I would try keeping the address book open until you're done reading from it. That is, don't call CFRelease(addressBook); until after reading all the phone numbers.

The address book is more like a database than a flat file and reading in a contact record gives you references to other fields & data rather than all of the actual field content.

Upvotes: 17

Related Questions