I want to display an ABPeoplePicker with only people who have a geographic address defined.
So I create an addressBook and remove people that dont have an address:
addressBook = ABAddressBookCreate();
NSArray *peopleList = (NSArray *)ABAddressBookCopyArrayOfAllPeople( addressBook );
NSLog(@"There are %d people in addressBook", ABAddressBookGetPersonCount(addressBook));
for (id peopleRecord in peopleList) {
ABMultiValueRef mv = ABRecordCopyValue((ABRecordRef)peopleRecord, kABPersonAddressProperty);
CFIndex numberOfAddresses = ABMultiValueGetCount(mv);
if( numberOfAddresses == 0 ) {
CFErrorRef err;
ABAddressBookRemoveRecord( addressBook, (ABRecordRef)peopleRecord, &err);
}
}
[peopleList release];
NSLog(@"There are now %d people in addressBook", ABAddressBookGetPersonCount(addressBook));
ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
NSNumber* addressProp = [NSNumber numberWithInt:kABPersonAddressProperty];
[peoplePicker setAddressBook:addressBook];
peoplePicker.displayedProperties = [NSArray arrayWithObject:addressProp];
[peoplePicker setPeoplePickerDelegate:self];
[self presentModalViewController:peoplePicker animated:YES];
For info, before filtering I have 125 records, and after filtering I have 93 records.
When I display the peoplePicker and scroll through it, it crashes with:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (49) beyond bounds (49)'
Any idea what's wrong?