开发者

How to get a list of contacts in address book only that have a fax number?

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.


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

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.

Here's some quick-n-dirty sample 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 );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜