15

I'm trying to fetch localized phone label value using CNContact.My attampt so far:

    NSError *error = nil;

    CNContactFetchRequest *fetchRequest =[[CNContactFetchRequest alloc] initWithKeysToFetch:keysToFetch];

    [addressBook enumerateContactsWithFetchRequest:fetchRequest error:&error usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {

       CNLabeledValue *phoneNumberValue = contact.phoneNumbers.firstObject;

       NSString *label = phoneNumberValue.label;
       NSLog(@"Phone Label: %@",label); //Logs value like _$!<Home>!$_

       CNPhoneNumber *phoneNumber = phoneNumberValue.value;
       NSString *phoneNumberString = phoneNumber.stringValue;

       NSLog(@"Phone No: %@",phoneNumberString);
    }];

The problem is the phone label returns raw value like _$!<Home>!$_, _$!<Mobile>!$_. But I need Plain text like Home, Mobile. Is there any way I can get the localized value using Contact frameworks. I don't want to use Addressbook as it is deprecated in iOS 9.

Poles
  • 3,585
  • 9
  • 43
  • 91

3 Answers3

21

use CNLabeledValues class method + localizedStringForLabel: and pass the label

example:

   CNLabeledValue *phoneNumberValue = contact.phoneNumbers.firstObject;
   NSString *label = phoneNumberValue.label;
   label = [CNLabeledValue localizedStringForLabel:label];
   NSLog(@"Phone Label: %@",label); //Logs value like home
Arash Etemad
  • 1,827
  • 1
  • 13
  • 29
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • Still getting `_$!!$_` – Poles Feb 19 '16 at 09:54
  • are the contacts from an exchange server by chance? – Daij-Djan Feb 19 '16 at 09:55
  • cause exchange doesn't really support custom labels... MIGHT be the issue then – Daij-Djan Feb 19 '16 at 09:56
  • Sorry, It was my mistake. this code works correctly. – Poles Feb 19 '16 at 09:59
  • 6
    Here's the updated `Swift 3` localized string without _$!<...>!$_: phone number: `CNLabeledValue.localizedString(forLabel: label)` email: `CNLabeledValue.localizedString(forLabel: label)` – KBog Feb 27 '18 at 17:44
  • @Daij-Djan My email labels are also formatted with this prefix/suffix. Perhaps they are added by syncing macOS and/or iOS Contacts with Google Contacts. The only exception is that email addresses labeled “Email” are NOT formatted with this prefix/suffix. – Nello Lucchesi Jun 01 '22 at 20:09
14

And here it is in Swift 3:

let displayNumbers = contact.phoneNumbers.map() {
    let label = CNLabeledValue<NSString>.localizedString(forLabel: $0.label ?? "")
    return label + ": \u{200E}" + $0.value.stringValue
}

Added unicode LeftToRight override, to ensure the number is not reversed on RTL languages.

jazzgil
  • 2,250
  • 2
  • 19
  • 20
2

add this line Contact Access

 if contact.isKeyAvailable(CNContactPhoneNumbersKey){

         for phoneNumber:CNLabeledValue in contact.phoneNumbers {
                        let number  = phoneNumber.value

                        let number2 = number.stringValue

                        let lable :String  =  CNLabeledValue<NSString>.localizedString(forLabel: phoneNumber.label! )

                        print("\(lable)  \(number.stringValue)")
                    }


                }
BHAVIK
  • 890
  • 7
  • 35