我想显示一个仅显示带有电子邮件的联系人的 ABAddressBook,所以我尝试了这样的操作:
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );
for( CFIndex emailIndex = 0; emailIndex < nPeople; emailIndex++ ) {
ABRecordRef person = CFArrayGetValueAtIndex( allPeople, emailIndex );
ABMutableMultiValueRef emailRef=ABRecordCopyValue(person, kABPersonEmailProperty);
int emailCount = ABMultiValueGetCount(emailRef);
if(emailCount == 0) {
ABAddressBookRemoveRecord(addressBook, person, NULL);
}
}
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.addressBook = addressBook;
picker.peoplePickerDelegate = self;
[self presentModalViewController:picker animated:YES];
控制器显示,但显示所有联系人,如果我选择没有电子邮件的联系人,我会崩溃。如果我调用了 ABAddressBookSave(),那么它会通过电子邮件删除所有联系人,但这是一个永久性更改,甚至会从系统联系人中删除它们。这样做的正确方法是什么?