swiftcontacts-framework

A custom labeled phone number


The Contacts Framework provides a kind of phoneNumbers label like this:

CNLabelPhoneNumberiPhone, CNLabelPhoneNumberMain, CNLabelPhoneNumberPager and others.

for contact in self.contact.phoneNumbers {
            if contact.label == CNLabelPhoneNumberiPhone {
                self.phoneNumberTextField.text = (contact.value as! CNPhoneNumber).stringValue
                break
            }
        }

But they're limited. There is no any options for a custom labeled phoneNumbers. Can I display a custom labeled phoneNumbers on a Contacts Framework ?

` if !self.contact.phoneNumbers.isEmpty {

        for contact in self.contact.phoneNumbers {

           switch contact.label {
            case CNLabelHome:
                self.phoneNumberTextField.text = (contact.value as! CNPhoneNumber).stringValue
            case CNLabelWork:
                self.phoneNumberTextField.text = (contact.value as! CNPhoneNumber).stringValue
            case CNLabelOther:
                self.phoneNumberTextField.text = (contact.value as! CNPhoneNumber).stringValue
            case CNLabelPhoneNumberMain:
                self.phoneNumberTextField.text = (contact.value as! CNPhoneNumber).stringValue
            case CNLabelPhoneNumberiPhone:
                self.phoneNumberTextField.text = (contact.value as! CNPhoneNumber).stringValue
            case CNLabelPhoneNumberMobile:
                self.phoneNumberTextField.text = (contact.value as! CNPhoneNumber).stringValue
            case CNLabelPhoneNumberPager:
                self.phoneNumberTextField.text = (contact.value as! CNPhoneNumber).stringValue
            case CNLabelPhoneNumberHomeFax:
                self.phoneNumberTextField.text = (contact.value as! CNPhoneNumber).stringValue
            case CNLabelPhoneNumberWorkFax:
                self.phoneNumberTextField.text = (contact.value as! CNPhoneNumber).stringValue
            case CNLabelPhoneNumberOtherFax:
                self.phoneNumberTextField.text = (contact.value as! CNPhoneNumber).stringValue
            default:
                self.phoneNumberTextField.text = (contact.value as! CNPhoneNumber).stringValue
                break
            }
       }
  }

`


Solution

  • So there is no direct way to get custom label numbers, so you can do like this to get custom label numbers :-

    if !contact.phoneNumbers.isEmpty {
      for number in contact.phoneNumbers {
          // This return All the phone Numbers
          if number.label == CNLabelPhoneNumberiPhone {
              //iPhone number.
          } else if number.label == CNLabelPhoneNumberMobile {
              //Mobile phone number.
          } else if number.label == CNLabelPhoneNumberMain {
              //Main phone number.
          } else if number.label == CNLabelPhoneNumberHomeFax {
              //Home fax number.
          } else if number.label == CNLabelPhoneNumberWorkFax {
              //Work fax number.
          } else if number.label == CNLabelPhoneNumberOtherFax {
            //Other fax number.
          } else if number.label == CNLabelPhoneNumberPager {
            //Pager phone number.
         } else {
            // Custome Phone Number
            // number.label -> to get custome label name
        }
      }
    }