using qualtrics in a SwiftUI app with a UIViewControllerRepresentable

Viewed 36

I'm trying to make a simple swiftui app using qualtrics and I'm trying to use a uiviewrepresentable to make it work

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

on let displayed = ... I keep getting the error "Cannot convert value of type 'QualtricsViewRep' to expected argument type 'UIViewController'", how can I return this code as a UIViewController to use in a swiftui app, or is there some other way I should be approaching this?

1 Answers

Unfortunately I don't have Qualtrics installed, but I have worked with it within UIKit. My assumption here is that you will need to create an instance of a UIViewController. This view controller is what the qualtrics view will present itself over.

Ultimately, you will return the view controller which contains the qualtrics view presented over it.

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