我希望我的用户通过从通讯簿中选择联系人的电子邮件来填写电子邮件字段。我不希望他们滚动未设置电子邮件的所有联系人,因此我想过滤具有电子邮件地址的联系人。
这是我到目前为止写的代码。我可以弄清楚谁有电子邮件地址,谁没有,但我不能告诉他们ABPeoplePickerNavigationController
只列出正确的联系人。是否不可能实现这一点,我的意思是我必须通过使用表格视图来实现我自己的联系人选择器类,还是这段代码有问题?
ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *peopleList = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSLog(@"%ld people exist in the addressBook", ABAddressBookGetPersonCount(addressBook));
for (id peopleRecord in peopleList) {
ABMultiValueRef mv = ABRecordCopyValue((ABRecordRef)peopleRecord, kABPersonEmailProperty);
CFIndex numberOfAddresses = ABMultiValueGetCount(mv);
if(numberOfAddresses == 0) {
CFErrorRef err;
ABAddressBookRemoveRecord( addressBook, (ABRecordRef)peopleRecord, &err);
}
}
[peopleList release];
NSLog(@"%ld people have an email", ABAddressBookGetPersonCount(addressBook));
ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
NSNumber* emailProp = [NSNumber numberWithInt:kABPersonEmailProperty];
[peoplePicker setAddressBook:addressBook];
peoplePicker.displayedProperties = [NSArray arrayWithObject:emailProp];
[peoplePicker setPeoplePickerDelegate:self];
[self presentModalViewController:peoplePicker animated:YES];