Cannot read kABPersonPhoneProperty

那年仲夏 提交于 2019-11-30 18:12:48

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/8630738/cannot-read-kabpersonphoneproperty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!