【问题标题】:using qualtrics in a SwiftUI app with a UIViewControllerRepresentable在带有 UIViewControllerRepresentable 的 SwiftUI 应用程序中使用 qualtrics
【发布时间】:2023-02-21 04:36:40
【问题描述】:

我正在尝试使用 qualtrics 制作一个简单的 swiftui 应用程序,并且我正在尝试使用 uiviewrepresentable 来使其工作

@main
struct QualtricsPocApp: App {
var body: some Scene {
    WindowGroup {
        ContentView()
    }
}

init() {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
            // i have the actual intercept id's here i just removed them
            Qualtrics.shared.initializeProject(brandId: "brand", projectId: "proj", extRefId: "ref", completion: { (myInitializationResult) in print(myInitializationResult);})

            return true

      }
   }
}


struct QualtricsViewRep: UIViewControllerRepresentable {

typealias UIViewControllerType = UIViewController

func makeUIViewController(context: Context) -> UIViewController {
    let vc = UIViewController()
    Qualtrics.shared.evaluateProject { (targetingResults) in
        for (interceptID, result) in targetingResults {
            if result.passed() {
                let displayed = Qualtrics.shared.display(viewController: self, autoCloseSurvey: true)
            }
        }
    }
}

在让显示 = ... 我不断收到错误“无法将‘QualtricsViewRep’类型的值转换为预期的参数类型‘UIViewController’”,如何将此代码作为 UIViewController 返回以在 swiftui 应用程序中使用,或者是否有一些其他方式我应该接近这个?

【问题讨论】:

    标签: ios swift swiftui uikit qualtrics


    【解决方案1】:

    不幸的是,我没有安装 Qualtrics,但我已经在 UIKit 中使用过它。我在这里的假设是您需要创建 UIViewController 的实例。这个视图控制器是 qualtrics 视图将展示自己的地方。

    最终,您将返回包含呈现在其上的 qualtrics 视图的视图控制器。

    struct QualtricsViewRep: UIViewControllerRepresentable {
         typealias UIViewControllerType = UIViewController
    
         func makeUIViewController(context: Context) -> UIViewController {
             let vc = UIViewController()
                Qualtrics.shared.evaluateProject { (targetingResults) in
                    for (interceptID, result) in targetingResults {
                        if result.passed() {
                             DispatchQueue.main.async {
                                let vc = UIViewController()
                                let displayed = Qualtrics.shared.display(viewController: vc, autoCloseSurvey: true)
                             }
                        }
                    }
                }
             return vc
         }
    
         func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
             // your code here
         }
    }
    

    【讨论】:

    • 这编译但它实际上并没有打开 qualtrics webview
    • @davisdoesntpost 还有一些其他因素可能会影响到这一点。 qualtrics 项目是否初始化成功?调用 evaluate intercept 方法时,是否满足所有条件以显示 qualtrics? UIKit 端的初始化发生在应用委托中。你在哪里初始化 qualtrics 项目?
    • 我在 App 初始值设定项中有
    • 我更新了我的答案以处理演示文稿异步。试一试。如果仍然无法正常工作,能否请您发布您的 qualtrics 初始化代码?
    • 仍然没有用,我把这个单位添加到最初的帖子
    【解决方案2】:

    Qualtrics SwiftUI 应用程序示例

    我必须在工作中实施它,所以我决定分享我的解决方案。

    QualtricsDemoApp

    import SwiftUI
    import Qualtrics
    
    @main
    struct QualtricsDemoApp: App {
        var body: some Scene {
            WindowGroup {
                ContentView()
                    .task {
                        Qualtrics.shared.initializeProject(brandId: "BRAND_ID", projectId: "PROJECT_ID", extRefId: "EXT_REF_ID") { myInitializationResult in
                            print(myInitializationResult)
                        }
                    }
            }
        }
    }
    

    内容视图

    import SwiftUI
    import Qualtrics
    
    struct ContentView: View {
        @State private var showFeedback = false
        var body: some View {
            VStack {
                Button("Show Qualtrics Feedback") {
                    showFeedback.toggle()
                }
                
                if showFeedback {
                    QualtricsFeedbackRepresentable()
                }
            }
            .font(.title)
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    
    struct QualtricsFeedbackRepresentable: UIViewControllerRepresentable {
        func makeUIViewController(context: Context) -> UIViewController {
            let vc = UIViewController()
            Qualtrics.shared.evaluateProject { (targetingResults) in
                for (_, result) in targetingResults {
                    if result.passed() {
                        Qualtrics.shared.display(viewController: vc)
                    }
                }
            }
            return vc
        }
        
        func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
        }
    }
    

    现在我只需要弄清楚为什么它不是英文的。 ?

    【讨论】:

      猜你喜欢
      • 2020-12-28
      • 2020-06-06
      • 1970-01-01
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-09
      • 1970-01-01
      相关资源
      最近更新 更多