【发布时间】:2019-12-05 01:48:39
【问题描述】:
我正在尝试使用 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 结帐编写自己的视图是一种选择,但我想避免这种情况。
谢谢!
【问题讨论】:
标签: ios swift uikit swiftui braintree