通过编程从通讯录中检索电子邮件地址的方法在iOS 6设备上似乎不再起作用。它在iOS 5中有效,并且奇怪的是,在iOS 6模拟器中仍然有效。是否有一种新的方式可以从用户的通讯录中编程检索联系人?
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
self.contacts = [[NSMutableArray alloc] init];
int contactIndex = 0;
for (int i = 0; i < nPeople; i++) {
// Get the next address book record.
ABRecordRef record = CFArrayGetValueAtIndex(allPeople, i);
// Get array of email addresses from address book record.
ABMultiValueRef emailMultiValue = ABRecordCopyValue(record, kABPersonEmailProperty);
NSArray *emailArray = (__bridge_transfer NSArray *)ABMultiValueCopyArrayOfAllValues(emailMultiValue);
[self.contacts addObject:emailArray];
}
为了澄清,上面的代码不会导致崩溃,它只是返回了空结果。ABAddressBookCopyArrayOfAllPeople返回一个空数组。谢谢!
loadContacts
的整个体系结构可以分派到一个本地创建的队列中。 - Christopher Pickslay