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]; } } }
source share