如何只获取具有地址字段的联系人

时间:2012-08-14 11:49:00

标签: iphone objective-c ios ios5

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

- (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,New York,TEST2 54-965684-85 j.V   街道印度,TEST3 95-95684-956 NIL,TEST3 20-95684-956 NIL,

     

现在我得到了这个输出:

     

TEST1 TEST2 TEST2 TEST2

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

适合我的代码在my Answer。您可以检查地址是否存在,如果没有,那么您可以跳过该联系。

为此,您必须在项目和.m文件中导入ABAddressbook框架工作。

祝你好运!!