0

I've been looking for a solution on this for a while now and can't find a thing on implementing this (screenshot provided below).

I'm creating a custom ContactsViewController which uses the CNContact framework for CRUD functionalities. All is clear on how to implement them, aside from choosing a phone number label. Is there such a thing as a picker view controller for this or should I implement it manually?

Screenshot of the phone number label editing screen of Contacts

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Lysdexia
  • 453
  • 8
  • 22
  • @TamásSengel yup, I was thinking this way too, was just wondering if there a list of labels to get from including the Custom Label part if ever the user adds one. – Lysdexia Nov 26 '18 at 01:02

1 Answers1

1

You should implement that screen manually. The screen on your screenshot is a UITableViewController with a grouped UITableView and a checkmark accessory indicator for the selected cell.

Here is the list for predefined phone number labels (from the Apple Developer Documentation):

╔════════════════════════════╦═════════════════════╗
║           String           ║     Description     ║
╠════════════════════════════╬═════════════════════╣
║ CNLabelHome                ║ Home label          ║
║ CNLabelWork                ║ Work label          ║
║ CNLabelPhoneNumberiPhone   ║ iPhone number       ║
║ CNLabelPhoneNumberMobile   ║ Mobile phone number ║
║ CNLabelPhoneNumberMain     ║ Main phone number   ║
║ CNLabelPhoneNumberHomeFax  ║ Home fax number     ║
║ CNLabelPhoneNumberWorkFax  ║ Work fax number     ║
║ CNLabelPhoneNumberOtherFax ║ Other fax number    ║
║ CNLabelPhoneNumberPager    ║ Pager phone number  ║
╚════════════════════════════╩═════════════════════╝

To display the localized names of these constants, use CNLabeledValue.localizedString(forLabel:) (thanks, OOPer):

Swift

let localizedLabelString = CNLabeledValue<NSString>.localizedString(forLabel: CNLabelPhoneNumberiPhone)
print(localizedLabelString) //iPhone

Objective-C

NSString *localizedLabelString = [CNLabeledValue localizedStringForLabel: CNLabelPhoneNumberiPhone];
NSLog(@"%@", localizedLabelString); //iPhone

If you want to create a custom label for a contact, just use an arbitrary string for the label's name:

let phoneNumber = CNPhoneNumber(stringValue: "+18001234567")
let labeledPhoneNumber = CNLabeledValue(label: "arbitrary string", value: phoneNumber)
contact.phoneNumbers.append(labeledPhoneNumber)
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
  • Thanks! This should work, but a follow-up question. How about those labels that added as a custom? – Lysdexia Nov 26 '18 at 03:01
  • @Lysdexia If the label's string value is not in the list of predefined constants, then it's a custom label. – Tamás Sengel Nov 26 '18 at 03:04
  • 1
    Thanks! I've accepted your answer, but a follow-up question. How can I manage the custom labels? Also, the iOS Contacts app has a label of "home", on the documentation it doesn't have it. Do you have any idea? – Lysdexia Nov 26 '18 at 09:14
  • @Lysdexia I added home and work labels to the table, thank you for mentioning this! Also, I've added a section about custom labels to the end. – Tamás Sengel Nov 26 '18 at 16:45
  • I haven't seen any discussion of custom labels in the developer documentation, presumably they are just any valid, displayable string. From a `ContactsUI` perspective, the limitation is that they aren't going to be localized. – benc Jan 30 '23 at 19:58