我正在尝试使用 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>!$_
.但我需要像 Home、Mobile 这样的纯文本。有什么方法可以使用 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/