Get label names for ABPerson properties

2019-08-20 08:15发布

问题:

I want to get all label names for properties of ABPerson object. For example: record ABPerson has three phone numbers defined: Mobile, Other, Work. I use labelAtIndex method to get label name but returned string contains needed value wrapped in characters $!!$. Instead of returning just "Mobile" I get these "_$!<" wrap-characters.

I have following code:

//person object points to ABPerson record from addressBook
ABMultiValue *phoneNumbers = [person valueForProperty:kABPhoneProperty];

NSUInteger count = [phoneNumbers count];

for (int i = 0; i < count; i++) {
    NSLog(@"Phone numbers label: %@ value: %@", [phoneNumbers labelAtIndex:i], [phoneNumbers valueAtIndex:i]);        
}

In log I get following:

2012-01-23 01:14:04.234 FixMyAddressBook[3667:707] Phone numbers label: _$!<Mobile>!$_ value: +327382738273
2012-01-23 01:14:04.370 FixMyAddressBook[3667:707] Phone numbers label: _$!<Work>!$_ value: +3293829328

Could someone point me please how can I get label names for properties without special characters?

回答1:

As far as I'm aware you need to get the localized label for that item, you'll need to make sure you're using the right reference code.

// Grab the right property first
ABMutableMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex phoneNumberCount = ABMultiValueGetCount(phoneNumbers);

for(int k = 0; k < phoneNumberCount; k++)
        {

        //Get phone number label by iterating across this
            CFStringRef phoneNumberValue = ABMultiValueCopyValueAtIndex( phoneNumbers, k );

CFStringRef phoneNumberLabel = ABMultiValueCopyLabelAtIndex(phoneNumbers, i);
            CFStringRef phoneNumberLocalizedLabel = ABAddressBookCopyLocalizedLabel( phoneNumberLabel );    
// converts "_$!<Work>!$_" to "work" and "_$!<Mobile>!$_" to "mobile"
//do whatever you want to do here
//release your references
        CFRelease(phoneNumberLocalizedLabel);
CFRelease(phoneNumberValue);
}