如何仅显示带有电话号码的联系人?

时间:2011-04-02 17:27:51

标签: iphone objective-c cocoa-touch

我有一个收件人选择器视图。但我想在我选择之前只显示有电话号码的联系人。

这是我获得模态视图的方式:

-(void)messageWillShowRecipientPicker{
    ABPeoplePickerNavigationController *picker = 
              [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;

    NSArray *displayedItems = 
                [NSArray arrayWithObject:[NSNumber 
                         numberWithInt:kABPersonPhoneProperty]];

    picker.displayedProperties = displayedItems;
    // Show the picker 
    [self presentModalViewController:picker animated:YES];
    [picker release]; 
}

知道怎么做吗?

2 个答案:

答案 0 :(得分:5)

我测试了这个,应该工作。可能要调整它^ - ^

ABAddressBookRef addressBook = ABAddressBookCreate();
    NSArray *allContacts = [(NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook)autorelease];
    for (int i =0; i < allContacts.count; i++) {
        ABRecordRef person = [allContacts objectAtIndex:i];
        if (person != nil) {
            ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
            if (ABMultiValueGetCount(phones) == 0) {
                CFErrorRef error = nil;
                ABAddressBookRemoveRecord(addressBook, person, &error);
                NSLog(@"Removing %@",(NSString *)ABRecordCopyCompositeName(person));
            }
            CFRelease(phones);
        }
    }
    CFErrorRef saveError = nil;
    ABAddressBookSave(addressBook, &saveError);

    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    picker.addressBook = addressBook;

    NSArray *displayedItems = 
    [NSArray arrayWithObject:[NSNumber 
                              numberWithInt:kABPersonPhoneProperty]];

    picker.displayedProperties = displayedItems;

    // Show the picker 
    [self presentModalViewController:picker animated:YES];

    CFRelease(addressBook);

答案 1 :(得分:2)

您可以使用NSPredicate来过滤数据,但您可能需要创建一个代理对象来处理AddressBook或协议。

在此处查看https://github.com/erica/ABContactHelper/blob/master/ABContactsHelper.m有关AddressBook和Apple的Predicate信息的协议示例http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/Articles/pUsing.html以及此处http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/Reference/NSPredicate.html

干杯,祝你好运! (^ _ ^)