如何阅读电话簿号码标签?

时间:2012-01-31 09:14:24

标签: ios abaddressbook abperson

我知道如何从ABRecordRef获取电话号码,但我现在想要的是获取号码的类型,即其标签为字符串:

const CFStringRef kABPersonPhoneIPhoneLabel;
const CFStringRef kABPersonPhoneMainLabel;
const CFStringRef kABPersonPhoneHomeFAXLabel;
const CFStringRef kABPersonPhoneWorkFAXLabel;
const CFStringRef kABPersonPhonePagerLabel;

以下是我获取数字的方法:

//get all phone numbers                   
NSArray *phoneNumbersArray = (NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty);
NSInteger numbersCounter = 0;
for(numbersCounter = 0; numbersCounter < [phoneNumbersArray count]; numbersCounter++)
{
     NSString currentPhoneNumber = [phoneNumbersArray objectAtIndex:indexPhoneNumber];

      // here i would like to read the type of phone number 
      // NSLog(@"NumberType:%@",numberType);                    
 }

我尝试了各种各样的事情并且我已经阅读了ABPerson Reference而且我不知道如何获取电话号码类型?

2 个答案:

答案 0 :(得分:15)

我已弄清楚如何阅读电话号码的本地化标签

//get all phone numbers
ABMultiValueRef phoneNumberMultiValue = ABRecordCopyValue(currentPerson, kABPersonPhoneProperty);
NSUInteger phoneNumberIndex;
for (phoneNumberIndex = 0; phoneNumberIndex < ABMultiValueGetCount(phoneNumberMultiValue); phoneNumberIndex++) {

    CFStringRef labelStingRef = ABMultiValueCopyLabelAtIndex (phoneNumberMultiValue, phoneNumberIndex);

    NSString *phoneLabelLocalized = (NSString*)ABAddressBookCopyLocalizedLabel(labelStingRef);

    NSString *phoneNumber  = (NSString *)ABMultiValueCopyValueAtIndex(phoneNumberMultiValue, phoneNumberIndex);
    //memory management
    [phoneLabelLocalized release];
    [phoneNumber release];
    CFRelease(labelStingRef);
}

答案 1 :(得分:2)

这是一个代码片段,可以创建一个人,添加2个电话联系人,然后显示如何获取电话属性的标签和值:

ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueIdentifier multivalueIdentifier;

ABMultiValueAddValueAndLabel(multi, @"(555) 555-1234",
                             kABPersonPhoneMobileLabel, &multivalueIdentifier);
ABMultiValueAddValueAndLabel(multi, @"(555) 555-2345",
                             kABPersonPhoneMainLabel, &multivalueIdentifier);

ABRecordRef aRecord = ABPersonCreate();

CFErrorRef anError = NULL;

ABRecordSetValue(aRecord, kABPersonPhoneProperty, multi, &anError);

CFRelease(multi);

multi = ABRecordCopyValue(aRecord, kABPersonPhoneProperty);

for (CFIndex i = 0; i < ABMultiValueGetCount(multi); i++)
{
    CFStringRef phoneNumber, phoneNumberLabel;

    phoneNumberLabel = ABMultiValueCopyLabelAtIndex(multi, i);
    phoneNumber      = ABMultiValueCopyValueAtIndex(multi, i);

    NSLog(@"%@ %@", (NSString *) phoneNumberLabel, (NSString *) phoneNumber);

    CFRelease(phoneNumberLabel);
    CFRelease(phoneNumber);
}

CFRelease(aRecord);

CFRelease(multi);

在代码中,它会迭代所有多值,并分别使用ABMultiValueCopyLabelAtIndexABMultiValueCopyValueAtIndex提取标签和数字。