Get a localized phone tag using the contact structure

I am trying to get the localized phone label value using CNContact.My attampt:

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 that the phone label returns the original value, for example _$!<Home>!$_ , _$!<Mobile>!$_ . But I need plain text, for example Home , Mobile . Is there a way to get a localized value using Contact interfaces. I do not want to use the address book because it is deprecated in iOS 9.

+5
source share
3 answers

use the CNLabeledValue method of class + 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>!$_ 
+14
source

And here he is in Swift 3:

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

An overridden Unicode function LeftToRight has been added to ensure that the number will not be canceled in RTL languages.

+6
source

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)") } } 
+1
source

Source: https://habr.com/ru/post/1243378/


All Articles