如何在地址簿中获取只有传真号的联系人列表?

时间:2022-11-23 09:17:15

I'd like to get a list of all contacts that have a fax number and only those contacts. Any contacts with just an email or just a phone number I do not want to show.

我想要一份有传真号码的联系人名单,而且只有那些联系人。任何只联系一封电子邮件或一个我不想显示的电话号码的联系人。

1 个解决方案

#1


4  

If you haven't already you'll want to browse the ABAddressBook Reference

如果您还没有浏览过ABAddressBook引用

Iterate through all the records of the address book, and get the kABPersonPhoneProperty. This is a multivalue property, so iterate through all its labels. If the work fax (kABPersonPhoneWorkFAXLabel) or home fax (kABPersonPhoneHomeFAXLabel) labels are present, get those values.

遍历地址簿的所有记录,并获取kABPersonPhoneProperty。这是一个多值属性,所以遍历它的所有标签。如果工作传真(kABPersonPhoneWorkFAXLabel)或家庭传真(kABPersonPhoneHomeFAXLabel)标签存在,则获取这些值。

Here's some quick-n-dirty sample code:

下面是一些快速-n-dirty示例代码:

ABAddressBookRef addressBook = ABAddressBookCreate();

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );

for( CFIndex personIndex = 0; personIndex < nPeople; personIndex++ ) {
    ABRecordRef person = CFArrayGetValueAtIndex( allPeople, personIndex );
    CFStringRef name = ABRecordCopyCompositeName( person );
    ABMultiValueRef phones = ABRecordCopyValue( person, kABPersonPhoneProperty );
    NSString* homeFax = nil;
    NSString* workFax = nil;
    BOOL hasFax = NO;
    for( CFIndex phoneIndex = 0; phoneIndex < ABMultiValueGetCount( phones ); phoneIndex++ ) {
        NSString* aLabel = (NSString*) ABMultiValueCopyLabelAtIndex( phones, phoneIndex );
        if( [aLabel isEqualToString:(NSString*)kABPersonPhoneHomeFAXLabel] ) {
        homeFax = (NSString*) ABMultiValueCopyValueAtIndex( phones, phoneIndex );
            hasFax = YES;
        }
        else if( [aLabel isEqualToString:(NSString*)kABPersonPhoneWorkFAXLabel]) {
            workFax = (NSString*) ABMultiValueCopyValueAtIndex( phones, phoneIndex );
            hasFax = YES;
        }
        [aLabel release];
    }
    if( hasFax ) {
        NSLog( @"%@: %@, %@", name, 
              homeFax == nil ? @"" : homeFax, 
              workFax == nil ? @"" : workFax );
        if( homeFax ) [homeFax release];
        if( workFax ) [workFax release];
    }
    CFRelease( phones );
    CFRelease( name );
}   

CFRelease( allPeople );
CFRelease( addressBook );

#1


4  

If you haven't already you'll want to browse the ABAddressBook Reference

如果您还没有浏览过ABAddressBook引用

Iterate through all the records of the address book, and get the kABPersonPhoneProperty. This is a multivalue property, so iterate through all its labels. If the work fax (kABPersonPhoneWorkFAXLabel) or home fax (kABPersonPhoneHomeFAXLabel) labels are present, get those values.

遍历地址簿的所有记录,并获取kABPersonPhoneProperty。这是一个多值属性,所以遍历它的所有标签。如果工作传真(kABPersonPhoneWorkFAXLabel)或家庭传真(kABPersonPhoneHomeFAXLabel)标签存在,则获取这些值。

Here's some quick-n-dirty sample code:

下面是一些快速-n-dirty示例代码:

ABAddressBookRef addressBook = ABAddressBookCreate();

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );

for( CFIndex personIndex = 0; personIndex < nPeople; personIndex++ ) {
    ABRecordRef person = CFArrayGetValueAtIndex( allPeople, personIndex );
    CFStringRef name = ABRecordCopyCompositeName( person );
    ABMultiValueRef phones = ABRecordCopyValue( person, kABPersonPhoneProperty );
    NSString* homeFax = nil;
    NSString* workFax = nil;
    BOOL hasFax = NO;
    for( CFIndex phoneIndex = 0; phoneIndex < ABMultiValueGetCount( phones ); phoneIndex++ ) {
        NSString* aLabel = (NSString*) ABMultiValueCopyLabelAtIndex( phones, phoneIndex );
        if( [aLabel isEqualToString:(NSString*)kABPersonPhoneHomeFAXLabel] ) {
        homeFax = (NSString*) ABMultiValueCopyValueAtIndex( phones, phoneIndex );
            hasFax = YES;
        }
        else if( [aLabel isEqualToString:(NSString*)kABPersonPhoneWorkFAXLabel]) {
            workFax = (NSString*) ABMultiValueCopyValueAtIndex( phones, phoneIndex );
            hasFax = YES;
        }
        [aLabel release];
    }
    if( hasFax ) {
        NSLog( @"%@: %@, %@", name, 
              homeFax == nil ? @"" : homeFax, 
              workFax == nil ? @"" : workFax );
        if( homeFax ) [homeFax release];
        if( workFax ) [workFax release];
    }
    CFRelease( phones );
    CFRelease( name );
}   

CFRelease( allPeople );
CFRelease( addressBook );