开发者

How to show only contacts that have address field

开发者 https://www.devze.com 2023-03-31 03:35 出处:网络
I want to show only those contacts having address field.I got number of rows in tableview as per all data count.can anybody help开发者_JAVA百科 me out?

I want to show only those contacts having address field.I got number of rows in tableview as per all data count.can anybody help开发者_JAVA百科 me out?

-(void)showPeoplePickerController
{

    ABAddressBookRef tempAddressBook = ABAddressBookCreate();
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(tempAddressBook);
    CFIndex nPeople = ABAddressBookGetPersonCount(tempAddressBook);

    for(NSUInteger i=0; i<nPeople; i++)
    {

        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);

        CFStringRef address;
        NSDictionary *add;
        ABMutableMultiValueRef multi = ABRecordCopyValue(ref, kABPersonAddressProperty); 
        NSLog(@" count is.. %ld",ABMultiValueGetCount(multi));
        if(ABMultiValueGetCount(multi)==0)
        {
            ABAddressBookRemoveRecord(tempAddressBook, ref, NULL);

        }
        for (CFIndex i = 0; i < ABMultiValueGetCount(multi); i++) 
        {           
            address = ABMultiValueCopyValueAtIndex(multi, i); 
            add=(NSDictionary *)address;
            NSLog(@"add is :- %@",add);

        } 

    }
    allPeople = ABAddressBookCopyArrayOfAllPeople(tempAddressBook);
    nPeople = ABAddressBookGetPersonCount(tempAddressBook);
    NSLog(@" No Of People ... %ld",nPeople);
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController      alloc]init];

    picker.peoplePickerDelegate = self;
    picker.addressBook =tempAddressBook;
//    [picker.searchDisplayController.searchResultsTableView numberOfRowsInSection:2];
    [self presentModalViewController:picker animated:YES];
    [picker release]; 
}


I had exactly the same issue. I ended up doing the following for filtering the addressbook:

// Get Copy of the address book.
ABAddressBookRef addressBook = ABAddressBookCreate();
// Get all persons in addressbook
NSArray * allPeople = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSMutableArray * filteredPeople = [[[NSMutableArray alloc]init]autorelease];
int i;
for (i = 0; i < [allPeople count]; i++) {
    // Get the actual person 
    ABRecordRef record = [allPeople objectAtIndex:i];
    bool gotAddress = NO;

    // Get the address properties.
    ABMutableMultiValueRef multiValue = ABRecordCopyValue(record, kABPersonAddressProperty);

    for(CFIndex j=0;j<ABMultiValueGetCount(multiValue);j++)
    {
        CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(multiValue, j);
        CFStringRef street = CFDictionaryGetValue(dict, kABPersonAddressStreetKey);
        CFStringRef zip = CFDictionaryGetValue(dict, kABPersonAddressZIPKey);
        CFStringRef city = CFDictionaryGetValue(dict, kABPersonAddressCityKey);
        CFRelease(dict);

        if(street != nil || zip != nil || city != nil)
            gotAddress = YES;
     }

    if(gotAddress){
        [filteredPeopleWithAddress addObject:record]; 
    }
}

after filtering in needed to create an own UITableView to present just those users to the users. I couldnt find a way to use the ABPeoplePickerNavigationController with the filtered addressbook. Maybe there is an easier way, but this worked for me.

0

精彩评论

暂无评论...
验证码 换一张
取 消