如何仅显示具有地址字段的联系人

时间:2011-08-26 09:51:16

标签: iphone

我想只显示那些有地址字段的联系人。我根据所有数据计数得到了tableview中的行数。有人帮帮我吗?

-(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]; 
}

1 个答案:

答案 0 :(得分:2)

我有完全相同的问题。我最后做了以下过滤地址簿:

// 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]; 
    }
}
过滤后需要创建一个自己的UITableView来向用户展示这些用户。我找不到将ABPeoplePickerNavigationController与过滤后的地址簿一起使用的方法。 也许有一种更简单的方法,但这对我有用。