SwiftUI x ContactsUI, add contact to group

Viewed 375

First of all, sorry for my English, I'm not a native speaker. If anything should be unclear, let me know.

In my project, there's a section for the user to organise contacts, but only a specific group of contacts. I could implement a custom struct or class for contacts, but the Contacts Framework by apple is nearly perfect. Also I'd like to use the devices calling, texting and emailing capabilities.

So I implemented the ContactsUI into my SwiftUI App:

struct addContactUIView : UIViewControllerRepresentable {
    func makeCoordinator() -> Coordinator {
        return Coordinator(self)
    }

    class Coordinator: NSObject, CNContactViewControllerDelegate, UINavigationControllerDelegate {

        private func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNMutableContact?) {
            if let c = contact {
                self.parent.contact = c
            }
            viewController.dismiss(animated: true)
        }
    
        func contactViewController(_ viewController: CNContactViewController, shouldPerformDefaultActionFor property: CNContactProperty) -> Bool {
            return true
        }

        var parent: addContactUIView

        init(_ parent: addContactUIView) {
            self.parent = parent
        }
    }

    @Binding var contact: CNMutableContact

    init(contact: Binding<CNMutableContact>) {
        self._contact = contact
    }

    typealias UIViewControllerType = CNContactViewController
   
    func makeUIViewController(context: UIViewControllerRepresentableContext<addContactUIView>) -> addContactUIView.UIViewControllerType {
        let viewController = CNContactViewController(forNewContact: CNContact())
        viewController.delegate = context.coordinator
        return viewController
    }

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

The ContactsUI framework provides me with default saving and editing options, which is really great.

The problem is: How could I add the new contact to a specific group in contacts? For example "customers". There's a 'addMember' function for this, but where should I implement this?

I'm stuck here, so I appreciate any help very much!

0 Answers
Related