Display ABAddressBook with email-only contacts

I want to display an ABAddressBook that only shows email contacts, so I tried something like this:

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

The controller shows, but all contacts are displayed, and if I select those who do not have a letter, I get a failure. If I call ABAddressBookSave (), it deletes all contacts using email, but it is a permanent change that even removes them from system contacts. What is the right way to do this?

+6
source share
5 answers

You may need to create an array of contacts that have email and then map them to the UITableViewController. This is a way to build such an array: fooobar.com/questions/923072 / .... You can use ABPersonViewController or ABUnknownPersonViewController to display contact information.

+1
source

Have you considered creating a temporary address book and filling it with contacts with email addresses?

0
source

You delete all contacts without an email address from the address book, but do not save the address book at the end. When the AddressBook is loaded from ABPeoplePickerNavigationController, all previous changes will be lost! Try the following:

 if (ABAddressBookHasUnsavedChanges(addressBook)) ABAddressBookSave(addressBook, NULL) 

By the way, don't forget the CFRelease () address book, allPeople and emailRef.

0
source

You tried to show email values โ€‹โ€‹only as follows:

 ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init]; picker.displayedProperties = @[@(kABPersonEmailProperty)]; 

Perhaps this already does what you want? Did not check though.

0
source

From iOS 8, ABPeoplePickerNavigationController has a property that solves your problem:

 @property (nonatomic,copy) predicateForEnablingPerson 

Sample code from an Apple document: https://developer.apple.com/library/prerelease/ios/samplecode/PeoplePicker/Listings/PeoplePicker_AAPL_8or7_EmailPickerViewController_m.html )

 ABPeoplePickerNavigationController* abPicker = [ABPeoplePickerNavigationController new]; abPicker.peoplePickerDelegate = self; //... if ([abPicker respondsToSelector:@selector(setPredicateForEnablingPerson:)]){ // The people picker will enable selection of persons that have at least one email address. abPicker.predicateForEnablingPerson = [NSPredicate predicateWithFormat:@" emailAddresses.@count > 0"]; } 

Please note that from iOS9 ABPeoplePavlerNavigationController is deprecated, you can use the new CNContactPickerViewController in the same way with the property:

 @property(nonatomic, copy) NSPredicate *predicateForEnablingContact 

From the doc:

You can set a value for this property to determine which contact should be available for selection, for example, an email address. @count> 0 to make all contacts that have an email address available for selection. If no predicate is specified for this property, all contacts become available.

0
source

Source: https://habr.com/ru/post/923071/


All Articles