Displaying ABAddressBook with contacts that have email only

Viewed 1322

I want to display a ABAddressBook that only shows contacts with an email, 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 the contacts are shown, and if I select the ones without an email, I get a crash. If I called ABAddressBookSave(), then it removes all contacts with an email, but it is a permanent change which even deletes them from the system contacts. What's the right way to do this?

5 Answers
Related