Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display only contacts with phone number?

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?

like image 735
UpCat Avatar asked Apr 02 '11 17:04

UpCat


People also ask

How do I show only phone numbers in Iphone contacts?

Just did this my iPhone5S & it worked - Go to contacts - select groups - Under Exchange or your email account simply select contacts & turn off suggested contacts - All email only contacts unless saved that way as a contact should go off. Show activity on this post.

How do I sort my contacts by phone number?

In addition, the Android app lets you organize your contacts more efficiently. Tap your avatar and go to Contacts app settings—under Display and Edit contacts, you'll be able to sort contacts by first name or last name, or to show or hide phonetic names.


2 Answers

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);
like image 173
Hobbes the Tige Avatar answered Sep 24 '22 20:09

Hobbes the Tige


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! (^_^)

like image 40
Kevin Bomberry Avatar answered Sep 26 '22 20:09

Kevin Bomberry