无法读取 kABPersonPhoneProperty
我面临一个(奇怪的)问题:我想检索联系人的电话号码,但由于某种原因,我无法这样做。
我曾经
ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *contacts = (NSArray*) ABAddressBookCopyArrayOfAllPeople(addressBook);
CFRelease(addressBook);
获取所有联系人的数组。然后我想使用
ABMultiValueRef ref = ABRecordCopyValue([contacts objectAtIndex:i], kABPersonPhoneProperty);
NSLog(@"%d",ref==NULL);
,但 ABRecordCopyValue
总是返回 nil
...
请注意,我能够检索有关联系人的其他信息:例如,使用
CFStringRef name = ABRecordCopyCompositeName([contacts objectAtIndex:i]);
提取姓名效果很好 有人可以解释一下我做错了什么吗?我正在使用带有 Xcode 4.2 的 Snow Leopard,并且正在为 iOS 4.0 进行开发...
没有使用而是
ABRecordCopyValue([contacts objectAtIndex:i], kABPersonPhoneProperty);
编辑:我找到了一个解决方案:我
ABRecordID idRec = ABRecordGetRecordID([contacts objectAtIndex:i]);
ABMultiValueRef ref = ABRecordCopyValue(ABAddressBookGetPersonWithRecordID(addressBook, idRec), kABPersonPhoneProperty);
使用了但是我必须保持对 addressBook
的引用有效(不要释放它),因此EricS建议的解决方案似乎更好。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这只是一个猜测,但我会尝试保持地址簿打开,直到您读完为止。也就是说,在读取所有电话号码之前不要调用
CFRelease(addressBook);
。地址簿更像是一个数据库,而不是一个平面文件,读取联系人记录可以为您提供对其他字段和信息的引用。数据而不是所有实际字段内容。
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.