skip to Main Content

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?

2

Answers


  1. 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
         }
    }
    
    Login or Signup to reply.
  2. Sample Qualtrics SwiftUI App

    I had to implement this for work so I decided to share my solution.

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

    ContentView

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

    Showing Qualtrics Feedback form

    Now I just have to figure out why it’s not in English. 😃

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search