iOS14 CNContactViewController not showing delete button issue

603 Views Asked by At

When I press Edit from contact card, my CNContactViewController is not showing the delete option in the bottom of the screen.

NB: the button remains shown for iOS 13.

screenshot

2

There are 2 best solutions below

1
On
import Foundation
import ContactsUI
import SwiftUI

struct CNContactViewControllerRepresentable: UIViewControllerRepresentable {
typealias UIViewControllerType = CNContactViewController
    var contact: Binding<CNContact>
    var presentingEditContact: Binding<Bool>

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

    func makeUIViewController(context: UIViewControllerRepresentableContext<CNContactViewControllerRepresentable>) -> CNContactViewControllerRepresentable.UIViewControllerType {
        let controller = CNContactViewController(forNewContact: contact.wrappedValue)
        controller.delegate = context.coordinator
        return controller
    }

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

    // Nested coordinator class, the prefered way stated in SwiftUI documentation.
    class Coordinator: NSObject, CNContactViewControllerDelegate {
        var parent: CNContactViewControllerRepresentable

        init(_ contactDetail: CNContactViewControllerRepresentable) {
            self.parent = contactDetail
        }

        func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) {
            parent.contact.wrappedValue = contact ?? parent.contact.wrappedValue
            parent.presentingEditContact.wrappedValue = false
        }

        func contactViewController(_ viewController: CNContactViewController, shouldPerformDefaultActionFor property: CNContactProperty) -> Bool {
            return true
        }
    }
}
0
On
.sheet(isPresented: $viewModel.presentingEditContact) {
            NavigationView {
                if #available(iOS 14, *) {
                    return AnyView(CNContactViewControllerRepresentable(contact: self.$viewModel.contact, presentingEditContact: $viewModel.presentingEditContact)
                        .navigationBarTitle("Edit Contact")
                        .edgesIgnoringSafeArea(.top))
                } else {
                    return AnyView(CNContactViewControllerRepresentable(contact: self.$viewModel.contact, presentingEditContact: $viewModel.presentingEditContact)
                        .edgesIgnoringSafeArea(.top))
                }
            }
        }