gpt4 book ai didi

ios - ViewController 委托(delegate)方法未从 AppDelegate 触发

转载 作者:行者123 更新时间:2023-11-28 15:11:20 24 4
gpt4 key购买 nike

我正在尝试为 Flutter 应用实现 Apple Pay。下面的代码成功呈现了正确填充的 Apple Pay View 。但是,当它尝试收费时,paymentAuthorizationViewController -> didAuthorizePayment 方法应该触发,但它没有触发。应该发生的是函数被命中,我将 token 发送回我的 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...
//}
})
}
}

最佳答案

您的 AppDelegate 不符合 PKPaymentAuthorizationViewControllerDelegate,因此转换失败,as? 返回 nil。因此,您的 paymentAuthorizationViewController 没有委托(delegate),因此不会调用它的委托(delegate)方法。您应该使您的 AppDelegate 符合 PKPaymentAuthorizationViewControllerDelegate,如下所示:

@objc class AppDelegate: FlutterAppDelegate, PKPaymentAuthorizationViewControllerDelegate{

关于ios - ViewController 委托(delegate)方法未从 AppDelegate 触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47584782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com