Beginner Beginner - 6 months ago 37
Swift Question

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
?

` func displayObjects() {

self.givenNameTextField.text = self.contact.givenName + " " + self.contact.familyName

if !contact.phoneNumbers.isEmpty {
for numbers in contact.phoneNumbers {
// Main phone number label
if numbers.label == CNLabelPhoneNumberMain {
self.phoneNumberTextField.text = (numbers.value as! CNPhoneNumber).stringValue
// iPhone phone number label
} else if numbers.label == CNLabelPhoneNumberiPhone {
self.phoneNumberTextField.text = (numbers.value as! CNPhoneNumber).stringValue
// Mobile phone number label
} else if numbers.label == CNLabelPhoneNumberMobile {
self.phoneNumberTextField.text = (numbers.value as! CNPhoneNumber).stringValue
// Home phone number label
} else if numbers.label == CNLabelHome {
self.phoneNumberTextField.text = (numbers.value as! CNPhoneNumber).stringValue
// Work phone number label
} else if numbers.label == CNLabelWork {
self.phoneNumberTextField.text = (numbers.value as! CNPhoneNumber).stringValue
// Other phone number label
} else if numbers.label == CNLabelOther {
self.phoneNumberTextField.text = (numbers.value as! CNPhoneNumber).stringValue
// Home fax phone number label
} else if numbers.label == CNLabelPhoneNumberHomeFax {
self.phoneNumberTextField.text = (numbers.value as! CNPhoneNumber).stringValue
// Work fax phone number label
} else if numbers.label == CNLabelPhoneNumberWorkFax {
self.phoneNumberTextField.text = (numbers.value as! CNPhoneNumber).stringValue
// Other fax phone number
} else if numbers.label == CNLabelPhoneNumberOtherFax {
self.phoneNumberTextField.text = (numbers.value as! CNPhoneNumber).stringValue
// Custom phone number label
} else {
self.phoneNumberTextField.text = (numbers.value as! CNPhoneNumber).stringValue
}

}
}


`

Answer

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
    }
  }
}