Pol*_*les 10 ios cncontact cncontactstore contacts-framework
我正在尝试使用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);
}];
Run Code Online (Sandbox Code Playgroud)
问题是手机标签返回原始值,如_$!<Home>!$_,_$!<Mobile>!$_.但我需要像Home,Mobile这样的纯文本.有什么办法可以使用Contact框架获取本地化值.我不想使用Addressbook,因为它在iOS 9中已弃用.
Dai*_*jan 16
使用CNLabeledValues类方法 + localizedStringForLabel:并传递标签
例:
CNLabeledValue *phoneNumberValue = contact.phoneNumbers.firstObject;
NSString *label = phoneNumberValue.label;
label = [CNLabeledValue localizedStringForLabel:label];
NSLog(@"Phone Label: %@",label); //Logs value like home
Run Code Online (Sandbox Code Playgroud)
这里是Swift 3:
let displayNumbers = contact.phoneNumbers.map() {
let label = CNLabeledValue<NSString>.localizedString(forLabel: $0.label ?? "")
return label + ": \u{200E}" + $0.value.stringValue
}
Run Code Online (Sandbox Code Playgroud)
添加了unicode LeftToRight覆盖,以确保RTL语言中的数字不会被反转.
| 归档时间: |
|
| 查看次数: |
2490 次 |
| 最近记录: |