1

我只想显示那些具有地址字段的联系人。我正在使用此代码..请帮助..

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

}

现在它在选择器中显示有地址的联系人,但现在我的输出是:

TEST1 44-541541-52 D-551,纽约,TEST2 54-965684-85 jV street India,TEST3 95-95684-956 NIL,TEST3 20-95684-956 NIL,

现在我得到这个输出:

测试1 测试2 测试2 测试2

请帮助...它显示重复..

4

2 回答 2

0

恐怕你运气不好。ABPeoplePickerController是一个相当有限的课程。

您将不得不滚动您自己的人员选择器控制器,并使用您之前迭代和过滤的地址簿中的所有记录填充它。

于 2012-08-14T11:57:52.080 回答
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 !!

于 2012-08-14T12:03:28.797 回答