0

Not able to get Delete Option in CNContactViewController in SwiftUI with UIViewControllerRepresentable.

struct CNContactEditControllerRepresentable: UIViewControllerRepresentable { typealias UIViewControllerType = CNContactViewController

var presentingEditContact: Binding<Bool>
var syncContact: Binding<Bool>
var contact: CNContact

func makeCoordinator() -> CNContactEditControllerRepresentable.Coordinator {
    Coordinator(self)
}

func makeUIViewController(context: UIViewControllerRepresentableContext<CNContactEditControllerRepresentable>) -> CNContactEditControllerRepresentable.UIViewControllerType {
    let controller = CNContactViewController.init(for: contact)
    controller.contactStore = CNContactStore()
    controller.delegate = context.coordinator
    controller.allowsEditing = true
    controller.allowsActions = true

    return controller
}

func updateUIViewController(_ uiViewController: CNContactEditControllerRepresentable.UIViewControllerType, context: UIViewControllerRepresentableContext<CNContactEditControllerRepresentable>) {
    //
}

// Nested coordinator class, the prefered way stated in SwiftUI documentation.
class Coordinator: NSObject, CNContactViewControllerDelegate {
    var parent: CNContactEditControllerRepresentable
    
    init(_ contactDetail: CNContactEditControllerRepresentable) {
        self.parent = contactDetail
    }
    
    func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) {
        if contact != nil {
            parent.syncContact.wrappedValue = true
        }
        parent.presentingEditContact.wrappedValue = false
    }
    
    func contactViewController(_ viewController: CNContactViewController, shouldPerformDefaultActionFor property: CNContactProperty) -> Bool {
        return true
    }
}

}

  • I have been looking for the answer too. I searched for how I could delete in Apple's docs too, and it's silent on deletion. Googling turns up just worthless results. The other thread like this one has gone unanswered for years I think. This is a sorry state of affairs. – Jason Cramer Jan 21 '22 at 00:56
  • I know this isn't a proper answer, but I found this question's answer to be useful if you need to manually delete a contact: https://stackoverflow.com/a/37225579/2127903 – Jason Cramer Jan 21 '22 at 01:18

0 Answers0