0

I am implementing a UIViewController with a ViewModel as an argument passed to the UIViewController, but I can't seem to make the delegate functions to work, what is the correct way of doing this?

CartView.swift

struct PaymentWrapper: UIViewControllerRepresentable {
    typealias UIViewControllerType = CustomUIPaymentViewController
    
    @ObservedObject var viewModel: CartViewModel
    
    var vc: CustomUIPaymentViewController?
    var foo: (String) -> Void
    public init(viewModel: CartViewModel) {
        self.viewModel = viewModel
        self.vc = CustomUIPaymentViewController.init(token: self.viewModel.mtToken)
    }

    func makeUIViewController(context: Context) -> CustomUIPaymentViewController {
        return vc!
    }

    func updateUIViewController(_ uiViewController: CustomUIPaymentViewController, context: Context) {
//        code
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(vc: vc!, foo: foo)
    }

    class Coordinator: NSObject, CustomUIPaymentViewControllerDelegate, CustomUINavigationControllerDelegate {
        var foo: (String) -> Void
        
        init(vc: CustomUIPaymentViewController, foo: @escaping (String) -> Void) {
            self.foo = foo
            super.init()
            vc.delegate = self
        }

        func paymentViewController(_ viewController: CustomUIPaymentViewController!, paymentFailed error: Error!) {
            foo("FAILED")
        }
            
        func paymentViewController(_ viewController: CustomUIPaymentViewController!, paymentPending result: TransactionResult!) {
            foo("PENDING")
        }
            
        func paymentViewController(_ viewController: CustomUIPaymentViewController!, paymentSuccess result: TransactionResult!) {
            foo("SUCCESS")
        }

        func paymentViewController_paymentCanceled(_ viewController: CustomUIPaymentViewController!) {
            foo("CANCEL")
        }

        //This delegate methods is added on ios sdk v1.16.4 to handle the new3ds flow
        func paymentViewController(_ viewController: CustomUIPaymentViewController!, paymentDeny result: TransactionResult!) {
        }
    }
}


struct CartView: View {
    @ObservedObject var viewModel = CartViewModel()
    
    var body: some View {
VStack {
            Header(title: "Cart", back: false)
        }
        .sheet(isPresented: $viewModel.showPayment) {
            PaymentWrapper(viewModel: self.viewModel) { data in
                print(data)
                // This returns error "Extra trailing closure passed in call"
            }
        }
    }
}

How do I get the delegate to work? what am I doing wrong? Thank you in advance.

Charas
  • 1,753
  • 4
  • 21
  • 53
  • you have to pass coordinator in makeUIViewController function```vc.delegate = context.coordionator``` – Raja Kishan Aug 18 '21 at 08:39
  • Okay that builds successfully now, but the delegate functions are still not firing up, what is missing? – Charas Aug 18 '21 at 08:52
  • Your delegate methods don't look right; I wouldn't expect to see implicitly unwrapped optionals. Some parameters, like `error` should be optionals – Paulw11 Aug 18 '21 at 08:53
  • And what is `UIPaymentViewController`? Is that your object? You shouldn't use `UI` prefix for your own classes; it is confusing – Paulw11 Aug 18 '21 at 08:56
  • sorry, the UIPaymentViewController is just an example, the real one in my codes have different names, I'll edit it right away, I'm using some third party UIPaymentViewController and trying to use their delegate functions, could you please help elaborate how is it supposed to be used? – Charas Aug 18 '21 at 08:58
  • I don't know how it is supposed to be used, because I don't know what "it" is; it looks like you are setting the delegate properly but without knowing how the delegate functions are declared I can't say if you have created them correctly. As I said, though, I would be surprised if the delegate methods were declared with implicitly unwrapped optionals. Can you at least show the protocol for the delegate? – Paulw11 Aug 18 '21 at 10:20
  • You advent set the `delegate` anywhere that you have shown. it would be something like `vc.delegate = context.coordinator` in the `make` function. – lorem ipsum Aug 18 '21 at 13:15

0 Answers0