【问题标题】:Flutter plugin not returning FlutterResult in swiftFlutter 插件没有快速返回 FlutterResult
【发布时间】:2021-07-16 11:46:09
【问题描述】:

所以目前我正在开发 Stripe 插件。

在 android 中一切正常。但是在ios中,我无法将其发送回flutter。

这是我在 IOS 中的插件文件

    public class SwiftStripeNativePlugin: NSObject, FlutterPlugin  {
    

    var flutterResults : FlutterResult!

    public static func register(with registrar: FlutterPluginRegistrar) {
    let channel = FlutterMethodChannel(name: "stripe_native", binaryMessenger: registrar.messenger())
    let instance = SwiftStripeNativePlugin()
    registrar.addMethodCallDelegate(instance, channel: channel)

    Stripe.setDefaultPublishableKey("*********")
  }
    
  public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {

    self.flutterResults=result;
    if(call.method.elementsEqual("lunch_stripe")){
        let args = call.arguments as? Dictionary<String,Any>
        setUpStripe(args: args! ,result:result);
    }
    else{
        result(FlutterMethodNotImplemented)
    }
  
}
    public func setUpStripe (args:Dictionary<String,Any>,result: @escaping FlutterResult){
        print(args);
        let myAPIClient=MyAPIClient();
        let host = HostController();
        
        myAPIClient.setupKeys(ephemeralKey: args["ephemeralKey"] as! [String:Any], clientSecret: args["clientSecret"] as! String, publishableKey: args["publishableKey"] as! String)

        Stripe.setDefaultPublishableKey(myAPIClient.publishableKey)
        host.myAPIClient=myAPIClient;
        host.flutterResults = result;
        
        
        if let navigationController = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController {
                    navigationController.pushViewController(host, animated: true)
                }

                let storyboard : UIStoryboard? = UIStoryboard.init(name: "Main", bundle: nil);
                let window: UIWindow = ((UIApplication.shared.delegate?.window)!)!

                let objVC: UIViewController? = storyboard!.instantiateViewController(withIdentifier: "FlutterViewController")
                let aObjNavi = UINavigationController(rootViewController: objVC!)
                window.rootViewController = aObjNavi
                aObjNavi.pushViewController(host, animated: true)
        
    }
   
    
}

这是我的 UIViewController 文件的名称是 HostController。我想将 didFinishWith 数据返回给颤振我尝试了多种方法,但没有返回数据。

 class HostController:UIViewController{
    var paymentContext: STPPaymentContext!
    var myAPIClient = MyAPIClient()
    var flutterResults: FlutterResult!
    var delegate:StripeDelegate!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let customerContext = STPCustomerContext(keyProvider:myAPIClient)
        let config=STPPaymentConfiguration.shared()
        
        config.appleMerchantIdentifier = ""
        config.companyName = ""
        
        config.requiredBillingAddressFields = .none
        config.requiredShippingAddressFields = .none
        config.additionalPaymentOptions=STPPaymentOptionType.all
        
        self.paymentContext = STPPaymentContext(customerContext: customerContext,configuration: config, theme: STPTheme.default());
        self.paymentContext.delegate = self
        self.paymentContext.hostViewController = self
        self.paymentContext.pushPaymentOptionsViewController()
        
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if(!paymentContext.loading){
        DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
            if(self.paymentContext.selectedPaymentOption != nil){
                self.didTapBuy()
                   }else{
                    self.popContoller()
                   }
       
               }
        }
    
    }
}
    

extension HostController:STPPaymentContextDelegate{
    func paymentContext(_ paymentContext: STPPaymentContext, didFailToLoadWithError error: Error) {
        NSLog("didFailToLoadWithError", error.localizedDescription)
          delegate.resposeToFlutter()
        print(error)
        
        self.flutterResults!(FlutterError(code: "Error", message: error.localizedDescription, details: error.localizedDescription))
        

    }
    
    func paymentContext(_ paymentContext: STPPaymentContext, didCreatePaymentResult paymentResult: STPPaymentResult, completion: @escaping STPPaymentStatusBlock) {
        //create payment
        NSLog("didCreatePaymentResult", paymentResult)
        let paymentIntentParams = STPPaymentIntentParams(clientSecret: myAPIClient.clientSecret)
            paymentIntentParams.paymentMethodId = paymentResult.paymentMethod.stripeId
        
        STPPaymentHandler.shared().confirmPayment(withParams: paymentIntentParams, authenticationContext: paymentContext) { status, paymentIntent, error in
                       switch status {
                       case .succeeded:
                           completion(.success, nil)
                       case .failed:
                            completion(.error, error) // Report error
                       case .canceled:
                           completion(.userCancellation, nil) // Customer cancelled
                       @unknown default:
                           completion(.error, nil)
                       }
                   }
    }
    
    func paymentContext(_ paymentContext: STPPaymentContext, didFinishWith status: STPPaymentStatus, error: Error?) {
        print(status);
        print( paymentContext)
        switch status {
           case .error:
            print("error");
           self.flutterResults!("Payment Error:" + error!.localizedDescription);
            self.popContoller();
            break;
            
           case .success:
            print("success");

            self.flutterResults!("Payment Sucess:");
            self.popContoller();
            break;
            
           case .userCancellation:
            print("userCancellation");
            self.flutterResults!("Payment error");
            self.popContoller();
            return // Do nothing
        @unknown default:
            fatalError()
        }
    }
   
    func paymentContextDidChange(_ paymentContext: STPPaymentContext) {
        print(paymentContext)
    
    }
    
    func popContoller(){
        self.navigationController?.popViewController(animated: true)
    }
    
     func didTapBuy() {
        self.paymentContext.requestPayment()
    }
    
    func toString(_ value: Any?) -> String {
      return String(describing: value ?? "")
    } 

【问题讨论】:

    标签: flutter stripe-payments flutter-plugin


    【解决方案1】:

    经过大量研究,我找到了可行的解决方案。 这里

    let viewController = UIApplication.shared.delegate?.window??.rootViewController
        
    viewController?.present(host, animated: true, completion: nil)
    

    现在您可以将 FlutterResult 回调到 Dart。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-28
      • 2020-02-13
      • 2014-12-03
      相关资源
      最近更新 更多