4

我知道如何从 an 中获取电话号码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 参考,但我不知道如何获取电话号码类型?

4

2 回答 2

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);
}
于 2012-02-01T07:38:01.963 回答
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);

在代码中,它遍历所有多值,并分别使用和提取标签和ABMultiValueCopyLabelAtIndex数字ABMultiValueCopyValueAtIndex

于 2012-01-31T13:39:35.407 回答