在SwiftUI视图中集成Persona SDK



我正在尝试将Person SDK v2集成到SwiftUI视图中。它是UIKit从特定UIViewController中呈现的设置。这是我的密码。https://docs.withpersona.com/docs/ios-sdk-v2-integration-guide

我不知道如何从SwiftUI调用我的present函数。SDK是这样设置的,所以当你创建Inquiry对象时,它会触发它在视图控制器上的导航。

struct PersonaInquiry: UIViewControllerRepresentable {
private var viewController = UIViewController()
private var coordinator = Coordinator()

class Coordinator: NSObject, InquiryDelegate {
func inquiryComplete(inquiryId: String, status: String, fields: [String : Persona2.InquiryField]) {

}

func inquiryCanceled(inquiryId: String?, sessionToken: String?) {

}

func inquiryError(_ error: Error) {

}
}

func makeUIViewController(context: Context) -> UIViewController {
return viewController
}

func updateUIViewController(_ uiViewController: UIViewController, context: Context) {

}

func present(templateId: String) {
let config = InquiryConfiguration(templateId: templateId)

// Create the inquiry with the view controller
// as the delegate and presenter.
Inquiry(config: config, delegate: coordinator).start(from: viewController)
}

func makeCoordinator() -> Coordinator {
return coordinator
}
}
struct PersonaInquiry_Previews: PreviewProvider {
static var previews: some View {
PersonaInquiry()
}
}

下面是一个示例

ContentView:

struct ContentView: View {

@State private var isPresentingSDK = false
@State private var message = ""

var body: some View {
VStack {
Button(
action: {
isPresentingSDK.toggle()
},
label: {
Text("Launch Inquiry from SwiftUI 🚀")
.foregroundColor(Color.white)
.padding()
}
)
.buttonStyle(.borderedProminent)
.buttonBorderShape(.capsule)
.fullScreenCover(
isPresented: $isPresentingSDK,
onDismiss: {
// Do nothing
},
content: {
InquirySDKWrapper(
inquiryComplete: { inquiryId, status, fields in
self.message = """
Inquiry Complete
Inquiry ID: (inquiryId)
Status: (String(describing: status))
"""
},
inquiryCanceled: { inquiryId, sessionToken in
self.message = "🤷‍♀️ Inquiry Cancelled"
},
inquiryErrored: { error in
self.message = """
💀 Inquiry Error
(error.localizedDescription)
"""
}
)
}
)

Text(message)
.multilineTextAlignment(.center)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

查询DKWrapper:

import Persona2
import SwiftUI
import UIKit
struct InquirySDKWrapper: UIViewControllerRepresentable {

/// The wrapper VC presents the SDK and acts as its delegate.
/// The delegate methods in turn call the callbacks in the WrapperDelegate
private let wrapperVC: WrapperViewController

/// Pass in the callbacks for each delegate method
init(
inquiryComplete: @escaping (String, String, [String: InquiryField]) -> Void,
inquiryCanceled: @escaping (_ inquiryId: String?, _ sessionToken: String?) -> Void,
inquiryErrored: @escaping (_ error: Error) -> Void
) {
wrapperVC = WrapperViewController()
wrapperVC.inquiryComplete = inquiryComplete
wrapperVC.inquiryCanceled = inquiryCanceled
wrapperVC.inquiryErrored = inquiryErrored
}

func makeUIViewController(context: Context) -> some UIViewController {
return wrapperVC
}

func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
// Do nothing
}
}

final class WrapperViewController: UIViewController {

private var isPresenting = false

// The callbacks
var inquiryComplete: ((_ inquiryId: String, _ status: String, _ fields: [String: InquiryField]) -> Void)!
var inquiryCanceled: ((_ inquiryId: String?, _ sessionToken: String?) -> Void)!
var inquiryErrored: ((_ error: Error) -> Void)!

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

// Otherwise this would trigger once the SDK exits too
guard !isPresenting else { return }

let inquiry = Inquiry(
config: InquiryConfiguration(
templateId: "YOUR TEMPLATE ID HERE"
),
delegate: self
)
inquiry.start(from: self)

isPresenting = true
}
}
extension WrapperViewController: InquiryDelegate {
func inquiryComplete(inquiryId: String, status: String, fields: [String: InquiryField]) {
inquiryComplete(inquiryId, status, fields)
dismiss(animated: true, completion: nil)
}
func inquiryCanceled(inquiryId: String?, sessionToken: String?) {
inquiryCanceled(inquiryId, sessionToken)
dismiss(animated: true, completion: nil)
}
func inquiryError(_ error: Error) {
inquiryErrored(error)
dismiss(animated: true, completion: nil)
}
}

相关内容

  • 没有找到相关文章