iOS ABPeoplePicker导航控制器自定义地址簿

时间:2012-02-02 21:18:57

标签: ios

我遇到了peoplePicker导航控制器的问题。我正在尝试过滤并仅显示与电子邮件的联系人。这似乎在中途工作。这是代码:

- (IBAction) addEmail: (id)sender {
    ABPeoplePickerNavigationController *picker;
    picker = [[ABPeoplePickerNavigationController alloc] init];

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

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

    picker.peoplePickerDelegate = self;

    [parentController presentModalViewController:picker animated:YES];

    [picker release];
}

- (ABAddressBookRef)getValidContacts{

    ABAddressBookRef allPeople = ABAddressBookCreate();
    ABAddressBookRef contactsWithEmail = 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 emailProperty = ABRecordCopyValue(aPerson, kABPersonEmailProperty);
        if(ABMultiValueGetCount(emailProperty)>0){
            NSLog(@"this dude has an email address, he's on the list");
            //ABAddressBookAddRecord(contactsWithEmail, aPerson, &anError);
       }
        else{
            NSLog(@"this guy has no email, removing them from the addressBook");
            ABAddressBookRemoveRecord(contactsWithEmail, aPerson, &anError);
        }

    }
    int foo = ABAddressBookGetPersonCount(contactsWithEmail);
    NSLog(@"at the end we have %d contacts with email", foo);
    return contactsWithEmail;
}

我在4个联系人的模拟器中对此进行测试:3个有一个或多个电子邮件地址,有一个没有。似乎我的getValidContacts方法做了它应该做的事情......它返回一个只有3个具有电子邮件属性的联系人的地址簿。当peoplePicker表出现时,第四个人仍在那里。如果我选择不存在的单元格,我会收到SIGABRT错误,就像我正在尝试触摸已解除分配的对象一样。

我已经读过,保存修改过的地址簿可以缓解我的问题,但我对删除其他人的联系人的前景并不感到疯狂。我想错了吗?如果我创建一个addressBook并保存它会修改设备上的共享地址簿吗?

任何人都有成功过滤地址簿的特定联系人可以指示我一点吗?

提前感谢。

0 个答案:

没有答案