【问题标题】:Integrating Persona SDK in a SwiftUI View在 SwiftUI 视图中集成 Persona SDK
【发布时间】:2022-09-23 07:46:57
【问题描述】:

我正在尝试将 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()
    }
}
  • 看看this setup,这是一种不同的方法。

标签: ios swift swiftui uiviewcontrollerrepresentable


【解决方案1】:

这是一个例子

内容视图:

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()
    }
}

查询SDKWrapper:

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)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    • 2023-03-05
    • 2020-07-15
    • 1970-01-01
    相关资源
    最近更新 更多