Reputation: 3519
I am using the Contact framework new to iOS 9 and I cannot figure out how to get the digits from the phoneNumbers key on a CNContact.
Doing an NSLog of the CNContact I get this output:
<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=(
)>
I am able to get the keys for givenName and familyName like this:
CNContact *contact;
[contact valueForKey:@"givenName"]
[contact valueForKey:@"familyName"]
How do I get the value for the digits that is under the phoneNumbers key?
Upvotes: 5
Views: 5733
Reputation: 304
I have a simpler way to segregate each phone number from the dictionary of phone numbers if you are picking single contacts from CNContactPickerViewController did select contact :-
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);
This way I am able to get this as the output :-
Home number is 4085553514 Mobile number is N/A Home FAX number is 4085553514
Upvotes: 0
Reputation: 318924
CNContact
has the phoneNumbers
property. Use that to get the array of phone numbers for the contact.
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
Upvotes: 16