Reputation: 70406
I have a recipient picker view. But I want to display only contacts that have a phone number before I pick one.
This is how I get the modal view:
-(void)messageWillShowRecipientPicker{
ABPeoplePickerNavigationController *picker =
[[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
NSArray *displayedItems =
[NSArray arrayWithObject:[NSNumber
numberWithInt:kABPersonPhoneProperty]];
picker.displayedProperties = displayedItems;
// Show the picker
[self presentModalViewController:picker animated:YES];
[picker release];
}
Any idea how to do that?
Upvotes: 5
Views: 2576
Reputation: 3821
I tested this out, should work. Might have to tweak it ^-^
ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *allContacts = [(NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook)autorelease];
for (int i =0; i < allContacts.count; i++) {
ABRecordRef person = [allContacts objectAtIndex:i];
if (person != nil) {
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
if (ABMultiValueGetCount(phones) == 0) {
CFErrorRef error = nil;
ABAddressBookRemoveRecord(addressBook, person, &error);
NSLog(@"Removing %@",(NSString *)ABRecordCopyCompositeName(person));
}
CFRelease(phones);
}
}
CFErrorRef saveError = nil;
ABAddressBookSave(addressBook, &saveError);
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
picker.addressBook = addressBook;
NSArray *displayedItems =
[NSArray arrayWithObject:[NSNumber
numberWithInt:kABPersonPhoneProperty]];
picker.displayedProperties = displayedItems;
// Show the picker
[self presentModalViewController:picker animated:YES];
CFRelease(addressBook);
Upvotes: 5
Reputation: 3122
You can use NSPredicate
to filter the data, but you may need to make a proxy object to deal with the AddressBook, or a Protocol.
Check out https://github.com/erica/ABContactHelper/blob/master/ABContactsHelper.m for an example of a Protocol for AddressBook and Apple's Predicate information here http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/Articles/pUsing.html and here http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/Reference/NSPredicate.html
Cheers and good luck! (^_^)
Upvotes: 2