Lysdexia
Lysdexia

Reputation: 443

CNLabelPhoneNumber Label Picker

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

Upvotes: 0

Views: 344

Answers (1)

glyvox
glyvox

Reputation: 58139

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)

Upvotes: 1

Related Questions