ios - 使用 SwiftUI 的 Braintree Drop-In

标签 ios swift uikit swiftui braintree

我正在尝试使用 Braintree 设置支付,但 Braintree 尚不支持 SwiftUI,因此我必须将其与 UIKit 集成。我使用 UIViewControllerRepresentable 创建了一个包装器,并使用 sheet 函数将其呈现为模态;但是,它没有按预期工作,它似乎打开了两个模式。

打开模式时的屏幕:enter image description here

这是我的包装器:

import SwiftUI
import BraintreeDropIn

struct BTDropInRepresentable: UIViewControllerRepresentable {
    var authorization: String
    var handler: BTDropInControllerHandler

    init(authorization: String, handler: @escaping BTDropInControllerHandler) {
        self.authorization = authorization
        self.handler = handler
    }

    func makeUIViewController(context: Context) -> BTDropInController {
        let bTDropInController = BTDropInController(authorization: authorization, request: BTDropInRequest(), handler: handler)!
        return bTDropInController
    }

    func updateUIViewController(_ uiViewController: BTDropInController, context: UIViewControllerRepresentableContext<BTDropInRepresentable>) {
    }
}

这是我尝试打开模式的地方:

Button(action: {
    self.checkout = true
}) {
    HStack {
        Spacer()
        Text("Checkout")
            .fontWeight(.bold)
            .font(.body)
        Spacer()
    }
    .padding(.vertical, 12)
    .foregroundColor(.white)
    .background(Color.blue)
}.sheet(isPresented: self.$checkout) {
    BTDropInRepresentable(authorization: self.token!, handler:  { (controller, result, error) in
                       if (error != nil) {
                           print("ERROR")
                       } else if (result?.isCancelled == true) {
                           print("CANCELLED")
                       } else if result != nil {
                        print("SUCCESS")
                           // Use the BTDropInResult properties to update your UI
                           // result.paymentOptionType
                           // result.paymentMethod
                           // result.paymentIcon
                           // result.paymentDescription
            }
        controller.dismiss(animated: true, completion: nil)
    })
}

有没有人在 SwiftUI 中使用过 Braintree 或遇到过类似情况?我是做错了什么还是忘记了什么? 我知道为 Braintree 结帐编写自己的 View 是一种选择,但我想避免这种情况。

谢谢!

最佳答案

查看您收到的错误,我敢打赌它与您需要添加的自定义 URL 方案有关:

Register a URL type

https://developers.braintreepayments.com/guides/paypal/client-side/ios/v4

此外,您还需要设置直接付款方式,我链接的指南中对此进行了详细说明。

关于ios - 使用 SwiftUI 的 Braintree Drop-In,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59187063/

相关文章:

ios - SwiftUI - 如何避免导航硬编码到 View 中?

iOS 使 VPN 服务器地址可变

ios - 在SwiftUI中从摇动手势(使用UIKit)设置EnvironmentObject不会更新View

ios - pjsip ios : Bluetooth not working

ios - 如何准确检测是否在 Swift 4 的 UILabels 中单击了链接?

iphone - UIView 3DCube 像 Clear-app

swift - 如何将特定日期的视频保存到相机胶卷?

ios - CLLocationManager.authorizationStatus 导致 EXC_BAD_ACCESS code=2

ios - UICollectionView 在刷新时重复和重新排序单元格内容

iphone - 将 UIButtons 添加到 Cocos2d 项目