1

I want to show only those contacts having address field.i am using this code.. please help..

- (ABAddressBookRef)getValidAddress{

    ABAddressBookRef allPeople = ABAddressBookCreate();
    ABAddressBookRef contactsWithAddress = ABAddressBookCreate();
    CFArrayRef allContacts = ABAddressBookCopyArrayOfAllPeople(allPeople);
    CFIndex numberOfContacts = ABAddressBookGetPersonCount(allPeople);
    CFErrorRef  anError = NULL; 

    for(int i=0; i<numberOfContacts;i++){
        ABRecordRef aPerson = CFArrayGetValueAtIndex(allContacts, i);
        ABMultiValueRef AddressProperty = ABRecordCopyValue(aPerson, kABPersonAddressProperty);
        if(ABMultiValueGetCount(AddressProperty)>0){
            NSLog(@"this dude has address, he's on the list");
            //ABAddressBookAddRecord(contactsWithEmail, aPerson, &anError);
        }
        else{
            NSLog(@"this guy has no address, removing them from the addressBook");
            ABAddressBookRemoveRecord(contactsWithAddress, aPerson, &anError);
        }

    }

    return contactsWithAddress;
}


- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar
{

    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];

    ABAddressBookRef test = [self getValidAddress];
    [picker setAddressBook:test];

    NSArray* emailProp = [NSArray arrayWithObjects:
    [NSNumber numberWithInt:kABPersonPhoneProperty], 
    [NSNumber numberWithInt:kABPersonEmailProperty],
    [NSNumber numberWithInt:kABPersonBirthdayProperty],
    [NSNumber numberWithInt:kABPersonAddressProperty],
    nil];;
    picker.displayedProperties = emailProp;

    picker.peoplePickerDelegate = self;
    [self presentModalViewController:picker animated:YES];
    //[parentController presentModalViewController:picker animated:YES];

    [picker release];

}

Now its showing contact in picker who have address but now my output is :

TEST1 44-541541-52 D-551, new york, TEST2 54-965684-85 j.V street India, TEST3 95-95684-956 NIL, TEST3 20-95684-956 NIL,

now i am getting this output:

TEST1 TEST2 TEST2 TEST2

Please Help... its showing duplicate..

iOS Developer
  • 103
  • 1
  • 8

2 Answers2

0

I am afraid you are out of luck. ABPeoplePickerController is a quite limited class.

You will have to roll your own people picker controller and populate it with all records from the address book which you have iterated through and filtered before.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • You are "creating" (i.e. copying) two address books in the first two lines. You should just maintain your own array of `ABRecordRef`s. – Mundi Aug 24 '12 at 06:45
0

The code which worked for me is in my Answer. You can check whether address exists or not, if not then you may skip that contact.

For this you have to import ABAddressbook frame work in your project and your .m file as well.

Best luck !!

Community
  • 1
  • 1
NSException
  • 1,268
  • 3
  • 14
  • 28