iOS: 从 CNContact 获取数字密钥
iOS: get digits key from CNContact
我正在使用 iOS 9 的新联系人框架,但我不知道如何从 CNContact 上的 phoneNumbers 键中获取数字。
对 CNContact 执行 NSLog 我得到以下输出:
<CNContact: 0x14f57e680: identifier=1B39B156-A151-4905-9624-
DB117ACFBADC, givenName=John, familyName=Doe,
organizationName=CompanyName, phoneNumbers=(
"<CNLabeledValue: 0x154297a40: identifier=3FEB6B0C-7179-4163-93E6-63C156C2F02B,
label=_$!<Mobile>!$_, value=<CNPhoneNumber: 0x155400e00: countryCode=us,
digits=1234567890>>"
), emailAddresses=(
), postalAddresses=(
)>
我可以像这样获取 givenName 和 familyName 的密钥:
CNContact *contact;
[contact valueForKey:@"givenName"]
[contact valueForKey:@"familyName"]
如何获取 phoneNumbers 键下数字的值?
CNContact
有 phoneNumbers
属性。使用它来获取联系人的 phone 号码数组。
CNContact *contact = ...;
NSArray <CNLabeledValue<CNPhoneNumber *> *> *phoneNumbers = contact.phoneNumbers;
CNLabeledValue<CNPhoneNumber *> *firstPhone = [phoneNumbers firstObject];
CNPhoneNumber *number = firstPhone.value;
NSString *digits = number.stringValue; // 1234567890
NSString *label = firstPhone.label; // Mobile
如果您从 CNContactPickerViewController 中选择单个联系人,我有一个更简单的方法将每个 phone 号码从 phone 号码的字典中分离出来 select 联系:-
NSString * phoneHome;
NSString * phoneMobile;
NSString * phoneHomeFax;
for (NSString* phoneNumber in contact.phoneNumbers){
NSString * phoneLabel = [phoneNumber valueForKey:@"label"];
if ([phoneLabel rangeOfString:@"Home"].location != NSNotFound){
phoneHome = [[phoneNumber valueForKey:@"value"] valueForKey:@"digits"];
}else{
phoneHome = @"N/A";
}
if ([phoneLabel rangeOfString:@"Mobile"].location != NSNotFound){
phoneMobile = [[phoneNumber valueForKey:@"value"] valueForKey:@"digits"];
}else{
phoneMobile = @"N/A";
}
if ([phoneLabel rangeOfString:@"HomeFAX"].location != NSNotFound){
phoneHomeFax = [[phoneNumber valueForKey:@"value"] valueForKey:@"digits"];
}else{
phoneHomeFax = @"N/A";
}
}
NSLog(@"\n Home number = %@ \n Mobile number = %@ \n Home FAX number = %@",phoneHome,phoneMobile,phoneHomeFax);
这样我就可以得到这个作为输出:-
家庭电话是4085553514
手机号码是 N/A
家庭传真号码是 4085553514
我正在使用 iOS 9 的新联系人框架,但我不知道如何从 CNContact 上的 phoneNumbers 键中获取数字。
对 CNContact 执行 NSLog 我得到以下输出:
<CNContact: 0x14f57e680: identifier=1B39B156-A151-4905-9624-
DB117ACFBADC, givenName=John, familyName=Doe,
organizationName=CompanyName, phoneNumbers=(
"<CNLabeledValue: 0x154297a40: identifier=3FEB6B0C-7179-4163-93E6-63C156C2F02B,
label=_$!<Mobile>!$_, value=<CNPhoneNumber: 0x155400e00: countryCode=us,
digits=1234567890>>"
), emailAddresses=(
), postalAddresses=(
)>
我可以像这样获取 givenName 和 familyName 的密钥:
CNContact *contact;
[contact valueForKey:@"givenName"]
[contact valueForKey:@"familyName"]
如何获取 phoneNumbers 键下数字的值?
CNContact
有 phoneNumbers
属性。使用它来获取联系人的 phone 号码数组。
CNContact *contact = ...;
NSArray <CNLabeledValue<CNPhoneNumber *> *> *phoneNumbers = contact.phoneNumbers;
CNLabeledValue<CNPhoneNumber *> *firstPhone = [phoneNumbers firstObject];
CNPhoneNumber *number = firstPhone.value;
NSString *digits = number.stringValue; // 1234567890
NSString *label = firstPhone.label; // Mobile
如果您从 CNContactPickerViewController 中选择单个联系人,我有一个更简单的方法将每个 phone 号码从 phone 号码的字典中分离出来 select 联系:-
NSString * phoneHome;
NSString * phoneMobile;
NSString * phoneHomeFax;
for (NSString* phoneNumber in contact.phoneNumbers){
NSString * phoneLabel = [phoneNumber valueForKey:@"label"];
if ([phoneLabel rangeOfString:@"Home"].location != NSNotFound){
phoneHome = [[phoneNumber valueForKey:@"value"] valueForKey:@"digits"];
}else{
phoneHome = @"N/A";
}
if ([phoneLabel rangeOfString:@"Mobile"].location != NSNotFound){
phoneMobile = [[phoneNumber valueForKey:@"value"] valueForKey:@"digits"];
}else{
phoneMobile = @"N/A";
}
if ([phoneLabel rangeOfString:@"HomeFAX"].location != NSNotFound){
phoneHomeFax = [[phoneNumber valueForKey:@"value"] valueForKey:@"digits"];
}else{
phoneHomeFax = @"N/A";
}
}
NSLog(@"\n Home number = %@ \n Mobile number = %@ \n Home FAX number = %@",phoneHome,phoneMobile,phoneHomeFax);
这样我就可以得到这个作为输出:-
家庭电话是4085553514 手机号码是 N/A 家庭传真号码是 4085553514