The goal is to present only certain fields when adding a new contact on iOS.
For instance, let's assume you only want to show and edit the address, phone number, and given name for a contact.
The code below doesn't work. All fields still appear.
Drop this view controller into a project, and you can see all contact fields are still presented, despite the use of displayedPropertyKeys.
How would you do this?
import Foundation
import Contacts
import ContactsUI
class ContactViewController: UIViewController, CNContactViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
func createContact() {
let contactController = CNContactViewController(forNewContact: nil)
contactController.delegate = self
contactController.allowsEditing = true
contactController.allowsActions = true
contactController.displayedPropertyKeys = [CNContactPostalAddressesKey, CNContactPhoneNumbersKey, CNContactGivenNameKey]
contactController.view.layoutIfNeeded()
present(UINavigationController(rootViewController: contactController), animated:true)
}
// =============================================================================================================
// MARK: IB Actions
// =============================================================================================================
@IBAction func newContactButtonDidTap(_ sender: UIButton) {
createContact()
}
// =============================================================================================================
// MARK: UIViewController Functions
// =============================================================================================================
override var prefersStatusBarHidden: Bool {
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}