Run ABAddressBook modal with email contacts only

I am trying to show a modal ABAddressBook only with contacts that have an email registered. How to achieve this?

I tried this code:

- (IBAction)getContact { // creating the picker ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init]; 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) { CFErrorRef error = nil; ABAddressBookRemoveRecord(addressBook, person, &error); if (error) NSLog(@"Error: %@", error); } } picker.addressBook = addressBook; picker.peoplePickerDelegate = self; [self presentModalViewController:picker animated:YES]; } 

The list is displayed with all my contacts, but “deleted” is displayed as “No name”, and those who have a name have a real email address.

+2
source share
1 answer

I managed to create another solution ...

I added contacts that have at least one email in the array ... Instead of the solution that will look in the current address book, and delete those that did not have email. Here is the code:

  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) { CFErrorRef error = nil; ABAddressBookRemoveRecord(addressBook, person, &error); if (error) NSLog(@"Error: %@", error); } else { ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty); NSString *email = (__bridge NSString *)ABMultiValueCopyValueAtIndex(emails, 0); NSString *name = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty)); if (name) { NSMutableDictionary *contactDict = [NSMutableDictionary dictionaryWithObjectsAndKeys: name, @"name", email, @"email", nil]; [self.contactsArray addObject:contactDict]; } } } 
+2
source

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


All Articles