【问题标题】:ViewController delegate methods not fired from AppDelegate未从 AppDelegate 触发的 ViewController 委托方法
【发布时间】:2018-05-15 01:13:16
【问题描述】:

我正在尝试为 Flutter 应用实施 Apple Pay。下面的代码成功显示了正确填充的 Apple Pay 视图。但是,当它尝试进行充电时,paymentAuthorizationViewController -> didAuthorizePayment 方法应该触发,但它不会触发。应该发生的是该函数被命中,我将令牌发送回我的 API 以从服务器创建费用。

我猜问题出在 paymentAuthorizationViewController.delegate = self as? PKPaymentAuthorizationViewControllerDelegate 行上,但我无法解决。

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {

    var flutterViewController: FlutterViewController!

    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
        ) -> Bool {

        GeneratedPluginRegistrant.register(with: self)

        flutterViewController = (window.rootViewController as? FlutterViewController)

        let stripeChannel = FlutterMethodChannel(name: "com.tram.gondola.ios/stripe", binaryMessenger: flutterViewController)
        stripeChannel.setMethodCallHandler({
            (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
            if ("handleApplePayButtonTapped" == call.method) {
                // create paymentRequest
                let paymentAmount: NSDecimalNumber = 12.34
                let merchantIdentifier = "merchant.com.merchantid"
                let paymentRequest = Stripe.paymentRequest(withMerchantIdentifier: merchantIdentifier, country: "US", currency: "USD")

                // Configure the line items on the payment request
                paymentRequest.paymentSummaryItems = [
                    // The final line should represent your company;
                    PKPaymentSummaryItem(label: "Company Name", amount: paymentAmount),
                ]

                if Stripe.canSubmitPaymentRequest(paymentRequest) {
                    // Setup payment authorization view controller
                    let paymentAuthorizationViewController = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest)
                    paymentAuthorizationViewController.delegate = self as? PKPaymentAuthorizationViewControllerDelegate

                    // Present payment authorization view controller
                    self.flutterViewController.present(paymentAuthorizationViewController, animated: true)
                }

            }
            else {
                // There is a problem with your Apple Pay configuration
                result(false)
            }

        })

        return super.application(application, didFinishLaunchingWithOptions: launchOptions);
    }

    func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: @escaping (PKPaymentAuthorizationStatus) -> Void) {
        print("didAuthorizePayment hit")
        STPAPIClient.shared().createToken(with: payment) { (token: STPToken?, error: Error?) in
            guard let token = token, error == nil else {
                // Present error to user...
                print("error")
                return
            }

            // send token back to flutter
            print(token)
            return
        }
    }

    func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
        // Dismiss payment authorization view controller
        controller.dismiss(animated: true, completion: {
            //if (paymentSucceeded) {
            // Show a receipt page...
            //}
        })
    }
}

【问题讨论】:

    标签: ios swift stripe-payments flutter


    【解决方案1】:

    您的AppDelegate 不符合PKPaymentAuthorizationViewControllerDelegate,因此那里的转换失败,as? 返回nil。因此,您的 paymentAuthorizationViewController 没有委托,因此它的委托方法不会被调用。你应该让你的AppDelegate符合PKPaymentAuthorizationViewControllerDelegate,像这样:

    @objc class AppDelegate: FlutterAppDelegate, PKPaymentAuthorizationViewControllerDelegate{
    

    【讨论】:

    • 你拯救了我的一天、我的一周和我的生命。欣赏它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 2011-06-18
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多