0

I am trying to implement a check marked action sheet in a SwiftUI View. I am using a UIViewControllerRepresentable to create a UIAlertController

struct WhatsAppAlertController: UIViewControllerRepresentable {
    let viewModel: PropViewModel

    func makeUIViewController(context: Context) -> UIAlertController {
        let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        let contactsNumbers = viewModel.contactsNumbers()

        for number in contactsNumbers {
            let action = UIAlertAction(
                title: "\(number.value.stringValue)",
                style: .default,
                handler: { _ in
                self.viewModel.openWhatsAppURL(withNumber: number.value.stringValue)
            })
            alert.addAction(action)
        }

        let cancel = UIAlertAction(title: L10n.cancel, style: .cancel, handler: nil)

        alert.addAction(cancel)

        return alert 
    }

    func updateUIViewController(_ uiViewController: UIAlertController, context: Context) {
    }
}

It is displayed using

.sheet(isPresented: $showWhatsAppActionSheet) {
            WhatsAppAlertController(viewModel: self.viewModel)
        }

I have a feeling it is because the UIAlertController is being presented using .sheet

My plan was to use action.setValue(true, forKey: "checked") to checkmark and remember the selected option.

Is there a way to fix this? Or perhaps implement the checkmark using only SwiftUI?

enter image description here

Cam Scen
  • 592
  • 1
  • 4
  • 9

1 Answers1

0

My mistake was not creating a holder controller, a UIViewController to house the UIAlertController. The presentation should also be done with .background() rather than .sheet()

Here is the updated code:

struct WhatsappAlertController: UIViewControllerRepresentable {
    @Binding var show: Bool
    let viewModel: PropViewModel

    func makeUIViewController(context: UIViewControllerRepresentableContext<WhatsappAlertController>) -> UIViewController {
        return UIViewController() // holder controller - required to present alert
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<WhatsappAlertController>) {
        if self.show {

            let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
            let contactsNumbers = viewModel.contactsNumbers()

            for number in contactsNumbers {
                let action = UIAlertAction(
                    title: "\(number.value.stringValue)",
                    style: .default,
                    handler: { _ in
                        self.viewModel.openWhatsAppURL(withNumber: number.value.stringValue)
                })
//                action.setValue(true, forKey: "checked")
                alert.addAction(action)
            }

            let cancel = UIAlertAction(title: L10n.cancel, style: .cancel, handler: nil)

            alert.addAction(cancel)

            DispatchQueue.main.async { // must be async !!
                uiViewController.present(alert, animated: true, completion: {
                    self.show = false  // hide holder after alert dismiss
                })
            }
        }
    }
}

And to display:

.background(WhatsappAlertController(show: self.$showWhatsAppActionSheet, viewModel: viewModel))

Cam Scen
  • 592
  • 1
  • 4
  • 9