Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying ABAddressBook with contacts that have email only

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?

like image 784
Snowman Avatar asked Aug 16 '12 20:08

Snowman


1 Answers

You might need to build an array of contacts that have email, then display it on UITableViewController. This is a way to build such array: https://stackoverflow.com/a/13980023/745862. You can use ABPersonViewController or ABUnknownPersonViewController to display contact details.

like image 134
catcyborg Avatar answered Oct 06 '22 19:10

catcyborg