我正在尝试访问iPad的地址簿电子邮件。
代码的一部分是:
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
ABRecordRef recordRef = ABAddressBookCopyDefaultSource(addressBookRef);
CFArrayRef arrayRef = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBookRef, recordRef, kABPersonSortByFirstName);
for(int i = 0;i<ABAddressBookGetPersonCount(addressBookRef);i++)
{
ABRecordRef ref = CFArrayGetValueAtIndex(arrayRef, i);
ABMultiValueRef emails = ABRecordCopyValue(ref, kABPersonEmailProperty);
.
.
.
.
崩溃只能在客户端iPad-Mini上重现。我们试图重现崩溃,但它没有发生。
在分析调试版本之后,我们知道崩溃发生在Line&#34; ABMultiValueRef电子邮件= ABRecordCopyValue(ref,kABPersonEmailProperty);&#34;。事故并非每次都发生。它发生在2-3次尝试访问地址簿电子邮件。仅限客户使用。
我的疑问是&#34; ABRecordRef ref = CFArrayGetValueAtIndex(arrayRef,i);&#34;可能是零或空。但在什么情况下,这可能是零或空?
任何人都可以帮助我知道崩溃的原因。
答案 0 :(得分:0)
原因可能是ABAddressBookGetPersonCount
返回地址簿中所有人的数量,而ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering
只返回1个来源的所有人,而且他们的数量可能不同。另外,我建议您检查下面的错误。当然你还有CFRelease
任何返回的CF对象。
CFErrorRef error = nil;
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, &error);
if (addressBookRef != nil) {
ABRecordRef recordRef = ABAddressBookCopyDefaultSource(addressBookRef);
CFArrayRef arrayRef = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBookRef, recordRef, kABPersonSortByFirstName);
const unsigned int nrAllPeopleInSource = (unsigned int)CFArrayGetCount (arrayRef);
if (arrayRef != nil) {
// for(int i = 0;i<ABAddressBookGetPersonCount(addressBookRef);i++)
/*
ABAddressBookGetPersonCount returns the number of all people in the address book, whereas
ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering returns all people of only 1 source, and their number might be different.
*/
for(int i = 0;i<nrAllPeopleInSource;i++)
{
ABRecordRef ref = CFArrayGetValueAtIndex(arrayRef, i);
ABMultiValueRef emails = ABRecordCopyValue(ref, kABPersonEmailProperty);
// ... further code
}
} else {
// no person found
}
} else {
// addressbook could not be opened, lookup error
}