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

170 views Asked by At

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
    }
}

}

0

There are 0 answers