0

I searched now for at least 2 hours and couldn't find an answer for this.

I need to have an object of type CNContact. I found the CNContactViewController and this view would meet perfectly my requirements. With it I can create a new contact with the predefined view and this gives me an CNContact object.

This is my problem: The CNContactViewController saves the created contact to the contactStore. But I don't want this. Can I suppress this behaviour? I'm pretty sure there must be a solution.

This is my code for creation of the ViewController:

let contactController = CNContactViewController(forNewContact: nil)

contactController.allowsEditing = true
contactController.allowsActions = false

contactController.displayedPropertyKeys = [CNContactPostalAddressesKey, CNContactPhoneNumbersKey, CNContactGivenNameKey]

contactController.delegate = self

contactController.view.layoutIfNeeded()

let navigationController = UINavigationController(rootViewController: contactController)
self.present(navigationController, animated: true)

Here I would like to use the contact without having it in the Addressbook:

func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) {
    viewController.navigationController?.dismiss(animated: true)
}

Thanks for your help!

Kaushik Makwana
  • 1,329
  • 2
  • 14
  • 24
  • Have you tried setting `contactController.contactStore = nil`? Alternatively you could just create your own view with address, phone and name fields and use the data to create a `CNMutableContact` – Paulw11 Feb 27 '19 at 03:09
  • Yes I tried `contactController.contactStore = nil`. I know that I could create my own view. But if there is already a `CNContactViewController` I prefer to use that. – Tim Müller Feb 27 '19 at 03:56
  • Matt's right, unfortunately you have to create your own form UI for what you want. Fortunately there are several OSS projects already out there you could use, or at least use the `CNContactViewController` as a template example to copy. – brandonscript Feb 27 '19 at 06:01

2 Answers2

1

But I don't want this. Can I suppress this behavior? I'm pretty sure there must be a solution.

There isn’t. If the user saves the edited contact (taps Done) then by the time you hear about it, it has been saved into the contacts database and there is nothing you can do about it.

Of course you can turn right around and delete the saved contact. But you cannot prevent the saving from taking place to begin with.

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

I'm doing something like this:

let contact = CNMutableContact()
contact.contactType = .person
contact.givenName = "giveName"

let cvc = CNContactViewController(forUnknownContact: contact)
cvc.delegate = self
cvc.contactStore = CNContactStore()
cvc.allowsEditing = false
self.navigationController?.pushViewController(cvc, animated: true)

You can allow the user to save the contact to the internal contact store - but it is not stored automatically.

Remember to implement the delegate functions (fx as an extension)

extension MyContactsViewController: CNContactViewControllerDelegate {
    func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) {
        viewController.dismiss(animated: true, completion: nil)
    }

    func contactViewController(_ viewController: CNContactViewController, shouldPerformDefaultActionFor property: CNContactProperty) -> Bool {
        print(property.key)
        return true
    }
}
Kim Rasmussen
  • 453
  • 1
  • 8
  • 21