Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: get digits key from CNContact

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?

like image 541
MSU_Bulldog Avatar asked Oct 30 '15 15:10

MSU_Bulldog


1 Answers

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
like image 83
rmaddy Avatar answered Oct 03 '22 05:10

rmaddy