0

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()
    }
}
Crashalot
  • 33,605
  • 61
  • 269
  • 439

1 Answers1

2

Let me attach a screenshot of the CNContactViewController class declaration: CNContactViewController

As you can see in the image, there is a @note there, and says: 'All properties are visible when editing the contact'. So I think that when you create a contact is being considered a special case of editing a contact.

I'm using the displayedPropertyKeys just to display a contact and is working good in that case.

Hope this helps you!

Fabrizio
  • 36
  • 5
  • So what you're saying is this is not possible? Thanks for the answer! – Crashalot Dec 04 '18 at 01:53
  • There is no note on the Apple documentation: https://developer.apple.com/documentation/contactsui/cncontactviewcontroller/1616915-displayedpropertykeys. Where did you see this note? – Crashalot Dec 04 '18 at 01:54
  • @Crashalot exactly, there is no note on the Apple documentation. Just open your Xcode, go to this line 'contactController.displayedPropertyKeys' and press cmd + click on displayedPropertyKeys, there you will be redirected to the CNContactViewController declaration and you will be able to see the comments in the displayedPropertyKeys definition. – Fabrizio Dec 04 '18 at 02:01