我正在尝试使用Braintree设置付款,但是Braintree尚不支持SwiftUI,因此我必须将其与UIKit集成。我使用UIViewControllerRepresentable
创建了一个包装器,然后使用sheet
函数将其呈现为模态;但是,它没有按预期工作,似乎正在打开两个模态。
这是我的包装纸:
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签出自己的视图,但是我想避免这种情况。
谢谢!
查看您收到的错误,我敢打赌,它与您需要添加的自定义URL方案有关:
注册URL类型
https://developers.braintreepayments.com/guides/paypal/client-side/ios/v4