How to get a CNContact phone number(s) as string in Swift?
Asked Answered
W

11

35

I am attempting to retrieve the names and phone number(s) of all contacts and put them into arrays with Swift in iOS. I have made it this far:

func findContacts() -> [CNContact] {

    marrContactsNumber.removeAllObjects()
    marrContactsName.removeAllObjects()

    let store = CNContactStore()

    let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey]

    let fetchRequest = CNContactFetchRequest(keysToFetch: keysToFetch)

    var contacts = [CNContact]()

    do {
        try store.enumerateContactsWithFetchRequest(fetchRequest, usingBlock: { (let contact, let stop) -> Void in
            contacts.append(contact)

            self.marrContactsName.addObject(contact.givenName + " " + contact.familyName)

            self.marrContactsNumber.addObject(contact.phoneNumbers)

            print(contact.phoneNumbers)
    }
    catch let error as NSError {
        print(error.localizedDescription)
    }

    print(marrContactsName.count)
    print(marrContactsNumber.count)

    return contacts
}

Once completed, marrContactsName contains an array of all my contacts' names exactly as expected. i.e. "John Doe". However, marrContactsNumber returns an array of values like

[<CNLabeledValue: 0x158a19950: identifier=F831DC7E-5896-420F-AE46-489F6C14DA6E,
label=_$!<Work>!$_, value=<CNPhoneNumber: 0x158a19640: countryCode=us, digits=6751420000>>,
<CNLabeledValue: 0x158a19a80: identifier=ECD66568-C6DD-441D-9448-BDEDDE9A68E1,
label=_$!<Work>!$_, value=<CNPhoneNumber: 0x158a199b0: countryCode=us, digits=5342766455>>]

I would like to know how to retrieve JUST the phone number(s) as a string value(s) i.e. "XXXXXXXXXX". Basically, how to call for the digit(s) value. Thanks!

Wednesday answered 31/3, 2016 at 20:38 Comment(0)
W
41

I found the solution: (contact.phoneNumbers[0].value as! CNPhoneNumber).valueForKey("digits") as! String

Wednesday answered 1/4, 2016 at 1:20 Comment(6)
valueForKey("digits" ) is not a public API, you should use the property stringValue : (contact.phoneNumbers.first?.value as? CNPhoneNumber)?.stringValueOutlawry
From , HamzaGhazouani in swift 3, ((contact.phoneNumbers.first?.value)! as CNPhoneNumber).stringValueNorbert
So this has worked for me in all cases except for when there is the country prefix in front ( ' +1' ). I've been digging into that but can't find what I'm looking for. Anyone know how to ONLY get the 10 digit US number?Hotel
Nevermind. I decided to do this : ` var phoneNumber = (number.value as! CNPhoneNumber).value(forKey: "digits") as! String if phoneNumber .contains("+1") { phoneNumber = "(phoneNumber.dropFirst(2))" } ` And I got the result I wantedHotel
Hello everyone, Can you tell me this code for xamarin.ios?Obloquy
FWIW I found the answer using the valueForKey("digits") give me better results in cleaner string formatting than the .stringvalue. Yes It's not a public API but I'll take my chances for now.Ploughman
D
24

you can get contact.phoneNumbers from CNLabeledValue:

for phoneNumber in contact.phoneNumbers {
  if let number = phoneNumber.value as? CNPhoneNumber,
      let label = phoneNumber.label {
      let localizedLabel = CNLabeledValue.localizedStringForLabel(label)
      print("\(localizedLabel)  \(number.stringValue)")
  }
}
Dishonorable answered 27/4, 2016 at 10:2 Comment(1)
One more hoop to jump through. Line 4 becomes let localizedLabel = CNLabeledValue<CNPhoneNumber>.localizedString(forLabel: label)Ottoottoman
A
15
/* Get only first mobile number */

    let MobNumVar = (contact.phoneNumbers[0].value as! CNPhoneNumber).valueForKey("digits") as! String
    print(MobNumVar)

/* Get all mobile number */

    for ContctNumVar: CNLabeledValue in contact.phoneNumbers
    {
        let MobNumVar  = (ContctNumVar.value as! CNPhoneNumber).valueForKey("digits") as? String
        print(MobNumVar!)
    }

 /* Get mobile number with mobile country code */

    for ContctNumVar: CNLabeledValue in contact.phoneNumbers
    {
        let FulMobNumVar  = ContctNumVar.value as! CNPhoneNumber
        let MccNamVar = FulMobNumVar.valueForKey("countryCode") as? String
        let MobNumVar = FulMobNumVar.valueForKey("digits") as? String

        print(MccNamVar!)
        print(MobNumVar!)
    }
Automat answered 24/6, 2016 at 10:38 Comment(1)
very handy (though somewhat unorthodox variable naming scheme)Desiccate
C
4

Here is how you do it in swift 4

func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty) {

    if let phoneNo = contactProperty.value as? CNPhoneNumber{
        txtPhone.text = phoneNo.stringValue
    }else{
        txtPhone.text=""
    }
}
Cp answered 27/2, 2018 at 17:16 Comment(1)
I used for selecting just "numberProperty" and this helped me. thanksStonework
D
4

Here's a Swift 5 solution.

import Contacts

func sendMessageTo(_ contact: CNContact) {

    let validTypes = [
        CNLabelPhoneNumberiPhone,
        CNLabelPhoneNumberMobile,
        CNLabelPhoneNumberMain
    ]

    let numbers = contact.phoneNumbers.compactMap { phoneNumber -> String? in
        guard let label = phoneNumber.label, validTypes.contains(label) else { return nil }
        return phoneNumber.value.stringValue
    }

    guard !numbers.isEmpty else { return }

    // process/use your numbers for this contact here
    DispatchQueue.main.async {
        self.sendSMSText(numbers)
    }
}

You can find available values for the validTypes array in the CNPhoneNumber header file.

They are:

CNLabelPhoneNumberiPhone
CNLabelPhoneNumberMobile
CNLabelPhoneNumberMain
CNLabelPhoneNumberHomeFax
CNLabelPhoneNumberWorkFax
CNLabelPhoneNumberOtherFax
CNLabelPhoneNumberPager
Depredation answered 11/9, 2018 at 17:15 Comment(0)
C
1

The definition of a CNLabeledValue:

The CNLabeledValue class is a thread-safe class that defines an immutable value object that combines a contact property value with a label. For example, a contact phone number could have a label of Home, Work, iPhone, etc.

CNContact.phoneNumbers is an array of CNLabeledValues and each CNLabeledValue has a label and a value.

To print the phoneNumbers corresponding to a CNContact you can try:

for phoneNumber in contact.phoneNumbers {
    print("The \(phoneNumber.label) number of \(contact.givenName) is: \(phoneNumber.value)")
}
Chophouse answered 31/3, 2016 at 23:56 Comment(1)
Thank you for the response. Now, phoneNumber.value is returning <CNPhoneNumber: 0x1502dcd00: countryCode=us, digits=+18062907644>. How can I get it to just show the digits?Wednesday
O
1

In swift 3 you can get direclty

 if item.isKeyAvailable(CNContactPhoneNumbersKey){
        let phoneNOs=item.phoneNumbers
        let phNo:String
        for item in phoneNOs{
            print("Phone Nos \(item.value.stringValue)")
        }
Oodles answered 25/11, 2016 at 10:53 Comment(0)
A
1

Keeping things simple:

let phoneNumbers: [String] = contact.phoneNumbers.compactMap { (phoneNumber: CNLabeledValue) in
    guard let number = phoneNumber.value.value(forKey: "digits") as? String else { return nil }
    return number
}
Alate answered 31/10, 2019 at 11:52 Comment(0)
E
1

for Swift 5+

func removeSpecialCharactersFromContactNumberOfUser(_ contactNo : String) -> String? {

    let digits = CharacterSet(charactersIn: "0123456789").inverted
    let modifiedContactNo = contactNo.components(separatedBy: digits).joined(separator: "")

    if modifiedContactNo.count > 9 {

        return modifiedContactNo

    } else {

        return nil
    }
}

var number = phone.value.stringValue
number = number.starts(with: "+91") ? number.replacingOccurrences(of: "+91", with: "") : number

if let formattedNumber = removeSpecialCharactersFromContactNumberOfUser(number)  {
    //use this formattedNumber                 
}

This is to remove +91 from your phone number and it's working fine.

Eft answered 14/2, 2020 at 7:30 Comment(0)
A
0

Swift 3 "_$!<Mobile>!$_" This item is written to create difference as well as putting a piece of opportunity to rely on various options.

for con in contacts
{
    for num in con.phoneNumbers
    {
        if num.label == "_$!<Mobile>!$_"    //Please Don't Change this!
        {
            self.contactNames.append(con.givenName)
            self.contactNums.append(num.value.stringValue)
            break
        }
        else
        {
            continue
        }
    }
}

Here we have num.value.stringValue

Arnie answered 23/2, 2017 at 4:43 Comment(1)
Hey @Aasim Putting a string in coding is a bad practice if you may let like CNLabelPhoneNumberMobile, CNLabelPhoneNumberiPhone, CNLabelPhoneNumberHomeFax.Disappointment
C
0

fetch without country code from phone contacts and also removed unwanted text such as dash, spaces etc.. and also post from phonetextfield import ContactsUI var phoneString:String!

func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
        let numbers = contact.phoneNumbers.first
        let a = (numbers?.value)?.stringValue ?? ""
        let myString = a
        let formattedString = myString.replacingOccurrences(of: " ", with: "")
        let newFormattedString = formattedString.replacingOccurrences(of: "(", with: "")
        let formatstring = newFormattedString.replacingOccurrences(of: ")", with: "")
        let  last10  = formatstring.replacingOccurrences(of: "-", with: "")
        phoneString = String(last10.suffix(10))
        phonetextField.text = phoneString
        
    }
    
    func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
        self.dismiss(animated: true, completion: nil)
    }
   @IBAction func inviteButton(_ sender : Any)
    {
        if phoneString == nil{
            phoneString =  phonetextField.text! //fetching from phonetextfield
            Phone = phoneString
        }
        else  {
            Phone = phoneString //fetching from phone contacts
        
        }
      }
Chelsae answered 8/4, 2021 at 11:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.