1

I'm getting the error:

Type 'NavView' does not conform to protocol 'UIViewRepresentable'

Here is my code. What should I do? Thanks. Using SwiftUI

struct NavView: UIViewRepresentable{

    func makeUIView(context: Context) -> NavigationViewController {
        var viewController: NavigationViewController

        // Define two waypoints to travel between
        let origin = Waypoint(coordinate: CLLocationCoordinate2D(latitude: 38.9131752, longitude: -77.0324047), name: "Mapbox")
        let destination = Waypoint(coordinate: CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365), name: "White House")

        // Set options
        let options = NavigationRouteOptions(waypoints: [origin, destination])

        // Request a route using MapboxDirections.swift
        Directions.shared.calculate(options) { (waypoints, routes, error) in
            guard let route = routes?.first else { return }
            // Pass the generated route to the the NavigationViewController
            viewController = NavigationViewController(for: route)
            viewController.modalPresentationStyle = .fullScreen

        }
    return viewController
    }

    func updateUIView(_ uiView: NavigationViewController, context: Context) {

    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
A4_TS
  • 130
  • 1
  • 14

1 Answers1

1

For this case it should be used UIViewControllerRepresentable, like below

struct NavView: UIViewControllerRepresentable {

    func makeUIViewController(context: Context) -> NavigationViewController {
        var viewController: NavigationViewController

        // Define two waypoints to travel between
        let origin = Waypoint(coordinate: CLLocationCoordinate2D(latitude: 38.9131752, longitude: -77.0324047), name: "Mapbox")
        let destination = Waypoint(coordinate: CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365), name: "White House")

        // Set options
        let options = NavigationRouteOptions(waypoints: [origin, destination])

        // Request a route using MapboxDirections.swift
        Directions.shared.calculate(options) { (waypoints, routes, error) in
            guard let route = routes?.first else { return }
            // Pass the generated route to the the NavigationViewController
            viewController = NavigationViewController(for: route)
            viewController.modalPresentationStyle = .fullScreen

        }
        return viewController
    }

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

    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • Thanks for pointing me towards the right direction. I came across another problem that maybe you'd like to help me with that's relevant to this post: https://stackoverflow.com/questions/61416364/how-to-return-value-out-of-completion-handler-special-case – A4_TS Apr 24 '20 at 19:51
  • @Asperi, How can i initialize NavigationViewController(). can you please help me with the code. – Programming Learner Jul 20 '21 at 15:04