ios - 使用联系人框架获取本地化电话标签

标签 ios cncontact cncontactstore contacts-framework

我正在尝试使用 CNContact 获取本地化电话标签值。到目前为止我的尝试:

    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>!$_ .但我需要像 HomeMobile 这样的纯文本。有什么方法可以使用 Contact 框架获取本地化值。我不想使用地址簿,因为它在 iOS 9 中已被弃用。

最佳答案

使用CNLabeledValue的类方法+ localizedStringForLabel:并传递标签

例子:

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

关于ios - 使用联系人框架获取本地化电话标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35501642/

相关文章:

swift - CNContactPickerViewController 中的多重选择

ios - CNContactStoreexecuteSaveRequest 失败,CNErrorDomain Code=101 No Accessible Writable Containers

ios - 如何使用 UICollectionView 创建 3 列

iphone - Reg : modifying layer that is being finalized. ...... [CALayer frame]: 发送到释放实例 0xe43c520 的消息

ios - Objective-C : How to remove a white background from a JPG image without losing edge quality

ios - 如何使用 CNContact 获取在 Objective - C 中最后修改的联系人?

iOS 9 - 获取的联系人返回空电话号码

ios - UITableView 委托(delegate)未被调用

ios - 如何添加联系人自定义应用程序帐户,如下图所示

iOS9 - CNContactPickerViewController : SelectionOfProperty is not working,(如果用户有多个电话号码)