How to get the contact list in the address book with a fax number only?

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

+3
source share
1 answer

If you do not want to browse the ABAddressBook Reference

Iterate over all address book entries and get kABPersonPhoneProperty. This is a multi-valued property, so repeat all of its labels. If work fax ( kABPersonPhoneWorkFAXLabel) or home fax ( kABPersonPhoneHomeFAXLabel) marks are present , obtain these values.

Here is sample code with fast n-dirty code:

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 );
+4
source

All Articles