无法读取kABPersonPhoneProperty [英] Cannot read kABPersonPhoneProperty

查看:148
本文介绍了无法读取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.

我用过

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);

ABRecordCopyValue 始终返回 nil ...

请注意,我能够检索有关联系人的其他信息:例如,使用

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]);



有人可以解释一下我做错了什么吗?我正在使用Snow Leopard和Xcode 4.2,我正在为iOS 4.0开发...


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...

编辑:我找到了一个解决方案:而不是使用

I found a solution: instead of using

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

我用过

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

但是我必须保持对 addressBook (不要发布它),因此EricS建议的解决方案似乎更好。

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

推荐答案

这只是猜测,但我会尝试保持地址簿处于打开状态,直到您完成阅读。也就是说,在阅读完所有电话号码之前,请不要调用 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.

这篇关于无法读取kABPersonPhoneProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆