5

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?

MSU_Bulldog
  • 3,501
  • 5
  • 37
  • 73
  • Did you look at the `phoneNumbers` property of `CNContact`? And why not use the `familyName` and `givenName` properties instead of using `valueForKey:`? – rmaddy Oct 30 '15 at 15:54
  • I was just showing that as an example of what I've tried. The phoneNumbers property returns an NSArray, but how do I get the digits value from the phoneNumbers array without some messy parsing of the array? – MSU_Bulldog Oct 30 '15 at 15:58
  • You don't. See my answer. Remember, a user can have several phone numbers. – rmaddy Oct 30 '15 at 15:59

2 Answers2

16

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
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thanks! Will this first value of the phoneNumbers array always be the mobile number? (assuming the mobile number exists) – MSU_Bulldog Oct 30 '15 at 16:02
  • Not at all. If a user has more than one phone number they could be in any order. You will have to look at the label of each. But keep in mind that there is no one specific label for a person's mobile number. Labels can be anything a user wants. While most will be some standard value representing a mobile phone, it could just as well be "Cute Bunny Phone". – rmaddy Oct 30 '15 at 16:05
  • Good to know, I'll need to make a loop to get all phone numbers if more than one exists. Thanks for the help! – MSU_Bulldog Oct 30 '15 at 16:11
  • 1
    i have tried and it working but in label value Lable : _$!!$_ & Contact : 888-555-551 is come like that, i want just Mobile so what to change in code? @rmaddy – Vivek Goswami Apr 07 '16 at 11:50
  • 3
    @VivekGoswami try using `[CNLabeledValue localizedStringForLabel:firstPhone.label]` – Tico Ballagas Apr 08 '16 at 00:13
  • @rmaddy is there any way to get countryCode=us from CNPhoneNumber – Urmi May 30 '17 at 22:22
  • We need it in C# for xamarin.ios native app development. – Sumit Mishra Jan 27 '20 at 11:20
0

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

Nishad Arora
  • 304
  • 1
  • 5
  • 12