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

时间:2011-05-22 14:35:19

标签: iphone ios

我想获得所有具有传真号码的联系人列表,并且只列出那些联系人。任何只有电子邮件的联系人或我不想展示的电话号码。

1 个答案:

答案 0 :(得分:4)

如果您还没有想要浏览ABAddressBook Reference

迭代地址簿的所有记录,然后获取kABPersonPhoneProperty。这是一个多值属性,因此遍历其所有标签。如果存在工作传真(kABPersonPhoneWorkFAXLabel)或家庭传真(kABPersonPhoneHomeFAXLabel)标签,请获取这些值。

这是一些快速脏的示例代码:

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