4

I have a TableView that present a contact. When I click the cell i would like to do straight to Editable view, instead of going to the CNContactViewController and press "edit Button". Now i have the following :

firstview

secondview

I would like to skip the second step. Is that possible?

the code I'm using is the same from apple's:

let contacts = try contactStore.unifiedContactWithIdentifier
        (contactIdentifier!, keysToFetch: toFetch)
    let controller = CNContactViewController(forContact: contacts)
        controller.contactStore = contactStore
        controller.delegate = self
        controller.editing = true

        navigationController?
            .pushViewController(controller, animated: true)
        print(controller.editButtonItem())

    } catch {
        print(error)
    }

EDIT: More or less, to illustrate, what Im trying to do, is the same as WhatsApp has in their App!, thanks!!

1 Answers1

3

Have you tried this approach ?

let toFetch = [CNContactViewController.descriptorForRequiredKeys()]

let contacts = try contactStore.unifiedContactWithIdentifier
        (contactIdentifier!, keysToFetch: toFetch)

let contactsViewController = CNContactViewController(forNewContact: contacts)
contactsViewController.delegate = self
contactsViewController.title = "Edit contact"
contactsViewController.contactStore = contactStore

self.navigationController?.pushViewController(contactsViewController, animated: true)
Jani
  • 1,400
  • 17
  • 36
  • Hi!! Thank you for the answer! but this function "CNContactViewController(forNewContact: contacts)" brings the view I want, except is to create a new contact, not to edit one that exists already. – Rafael Fernandes Mar 21 '16 at 20:53
  • If you pass in an existing contact for the newContact parameter it would be to edit that particular contact. This was the behaviour I experienced with my testing – Jani Mar 21 '16 at 20:57
  • but then when the user finishes editing and click "save", wouldn't duplicate that contact? – Rafael Fernandes Mar 23 '16 at 14:38
  • I tested with a very limited amount of contacts so that I could identify duplicates easily but I could not seem to see any contacts being duplicated. Did you notice anything like that ?. I wonder if there is any other solution for this problem. – Jani Mar 23 '16 at 22:35
  • Hi Jani! It actually worked! Thank you very much!! The only thing I have to set now is how to go back. – Rafael Fernandes Mar 24 '16 at 04:53
  • Glad it worked. Do you mean to go back when you press cancel ? – Jani Mar 24 '16 at 04:54
  • yeah, Implementing the exactly way you wrote above, when i go to the edit view, I can't go back (press cancel), the only way to exit the view is by pressing "done". i have to find a way to go around this issue, but once again thank you!! – Rafael Fernandes Mar 24 '16 at 04:56
  • Your delegate method func contactViewController(viewController: CNContactViewController, didCompleteWithContact contact: CNContact?). Should be called when you tap the cancel button. Simply pop the view controller from the navigationController like self.navigationController?.popViewController(true) – Jani Mar 24 '16 at 05:03
  • It's working perfectly with ContactUI framework. Is it possible to do this with AddressBookUI framework also? – Hamza Ansari Sep 09 '16 at 07:23