ios – 使用Contacts框架获取本地化的电话标签

我正在尝试使用CNContact.My获取本地化的电话标签值到目前为止:
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);
    }];

问题是手机标签返回的原始值如_ $!< Home>!$_,_ $!< Mobile>!$_.但我需要像Home,Mobile这样的纯文本.有什么办法可以使用Contact框架获取本地化值.我不想使用Addressbook,因为它在iOS 9中已被弃用.

解决方法

使用CNLabeledValues类方法localizedStringForLabel:并传递标签

例:

CNLabeledValue *phoneNumberValue = contact.phoneNumbers.firstObject;
   NSString *label = phoneNumberValue.label;
   label = [CNLabeledValue localizedStringForLabel:label];
   NSLog(@"Phone Label: %@",label); //Logs value like _$!<Home>!$_

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...