0

我使用第三方支付库,我将展示/呈现由第三方库创建的 ViewController。用户选择付款后,它将通过委托给出结果(字符串)。

我像这样实例化第三方视图控制器

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate{

  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    
    let flutterViewController : FlutterViewController = window?.rootViewController as! FlutterViewController
    
    
    setUpThirdPartyPayment(flutterViewController: flutterViewController)
    
    
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
 


  func setUpThirdPartyPayment(flutterViewController: FlutterViewController) {
    
    let channel = FlutterMethodChannel(name: "payment", binaryMessenger: flutterViewController.binaryMessenger)
    channel.setMethodCallHandler({ (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
        
        if (call.method == "openThirdPartyPayment") {
            let vc = ThirdPartyPaymentViewController()  // instantiate third party VC
            vc.paymentDelegate = flutterViewController // set FlutterViewController as the delegate
            controller.present(vc, animated: true) // show it
        } else {
            result(FlutterMethodNotImplemented)
        }

    })

  }

    
    
}

我从第三方代表那里收到了结果,所以我FlutterViewController像这样扩展以符合ThirdPartyPaymentViewControllerDelegate协议

extension FlutterViewController : ThirdPartyPaymentViewControllerDelegate {
    
    func onFinished(paymentResult: PaymentResult!) {

        let paymentStatus : String = paymentResult.status
       
        // I need to bring this paymentStatus string back to my flutter code
        // I need to use : result(paymentStatus)
        // but I am confused how to get access to `result`(FlutterResult) in here
        
    }
    
}

如您所见,我得到paymentStatus(字符串),我需要将其发送回我的 Flutter 代码。我知道我必须使用result: @escaping FlutterResult(完成处理程序),但我很困惑如何result在委托模式中使用。

请帮忙 ....

如果我所做的是完全错误的(扩展FlutterViewController),那么请用正确的方法帮助我:)

4

0 回答 0