简体   繁体   English

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

[英]How to display only contacts with phone number?

I have a recipient picker view. 我有一个收件人选择器视图。 But I want to display only contacts that have a phone number before I pick one. 但我想在我选择一个之前只显示有电话号码的联系人。

This is how I get the modal view: 这是我得到模态视图的方式:

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

Any idea how to do that? 知道怎么做吗?

I tested this out, should work. 我测试了这个,应该工作。 Might have to tweak it ^-^ 可能要调整它^ - ^

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);

You can use NSPredicate to filter the data, but you may need to make a proxy object to deal with the AddressBook, or a Protocol. 您可以使用NSPredicate来过滤数据,但您可能需要创建一个代理对象来处理AddressBook或协议。

Check out https://github.com/erica/ABContactHelper/blob/master/ABContactsHelper.m for an example of a Protocol for AddressBook and Apple's Predicate information here http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/Articles/pUsing.html and here http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/Reference/NSPredicate.html 查看https://github.com/erica/ABContactHelper/blob/master/ABContactsHelper.m ,获取AddressBook和Apple的Predicate信息的协议示例http://developer.apple.com/library/mac/#documentation /Cocoa/Conceptual/Predicates/Articles/pUsing.htmlhttp://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/Reference/NSPredicate.html

Cheers and good luck! 干杯,祝你好运! (^_^) (^_^)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM