Filtering entries in ABAddressBook / ABPeoplePicker NavigationController?

Discussion in 'iOS Programming' started by dbredesen, Aug 25, 2008.

  1. dbredesen macrumors newbie

    Aug 17, 2008
    I am trying to filter the entries in the Address Book when it pops up using ABPeoplePickerNavigationController. I don't want to save the filtered list to the system address book, but I want to just use the filtered list temporarily to display / choose in ABPeoplePickerNavigationController. Here is a simplified version of the code that I'm using:

    - (ABAddressBookRef) getFilteredAddressBook {
    	/// open the default address book. 
    	ABAddressBookRef addressBook = ABAddressBookCreate();
    	CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
    	CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
    	for (int i=0;i < nPeople;i++) { 
    		ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);	
    		if (/* some condition */) {
    			ABAddressBookRemoveRecord(addressBook, ref, nil);
    	return addressBook;
    - (IBAction)showFilteredAddressBook:(id)sender {
    	ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
    	peoplePicker.addressBook = [self getFilteredAddressBook];
    	[peoplePicker setPeoplePickerDelegate:self];
    	[self presentModalViewController:peoplePicker animated:YES];
    This code seems to run fine, however when showFilteredAddressBook() calls presentModalViewController, I get the following error:

    -[NSArray subarrayWithRange:]: index (5) beyond bounds (5)'
    I verified that some of my entries do meet the criteria, and hence some records are being removed. It appears however that somehow an array count inside the ABAddressBook is not getting updated properly.

    Has anyone done something similar? Am I missing something? Any help would be greatly appreciated.

    Dave Bredesen
  2. kainjow Moderator emeritus


    Jun 15, 2000
    It's not good to remove items from an array you're iterating over. I'd suggest creating a second address book and copy the items you do want into that.
  3. iphone.newbie macrumors newbie

    Apr 1, 2009
    Has this problem been solved ?


    I recently started iphone application development. And have the same requirement of allowing the user to select certain entries from the people picker. But i want to customize my address book by adding some more entries from an external source before I render the view. Since the AbAddRecord method does not save the change to the addressbook unless you save it, I am stuck on how to proceed.

    kainjow has suggested to make a copy of the addressbook and modify that. Can you give me some snippet on how that can be done ? I don't want to write the new entries to the default address book. Only want to keep them in memory.

    I know that worst-case we can get all the records, do any modification on them and then use TableViewController to render the entries. But then in this case, the UI would not be consistent with the default iPhone UI provided by ABPeoplePickerNavigationController.

    I would also like to show selected icons against the entries selected by the user. Is going the TableViewController the only way ?
    Or can I customize the ABPeoplePickerController to display additional entries per row on selection / deletion ?

    Has someone done anything like this ?

    iphone Newbie

Share This Page