UI components
The Navigation SDK for iOS is only available upon request. Contact us to get started.
Navigation View
Introduction
Usually the navigation process is followed using a visual representation of a navigation context (estimated time of arrival, current speed, progress, etc).
The Navigation SDK provides a UI component NavigationView
built on top of modern technologies to allow you to integrate the Navigation visualization into your app in a few steps.
In the examples we will use a typealias for NavigationView
named TomTomNavigationView
.
Integrating NavigationView within the SwiftUI codebase
Since the NavigationView is built with the SwiftUI, you can use it the same way as you use other SwiftUI components within your codebase.
Navigation ViewModel creation and initialization
1extension ContentView {2 final class ViewModel: ObservableObject {3 // MARK: Lifecycle45 // ...67 init(8 navigation: TomTomNavigation,9 navigationViewModel: TomTomNavigationView.ViewModel10 ) {11 self.navigation = navigation12 self.navigationViewModel = navigationViewModel13 }1415 convenience init() {16 let navigationConfiguration = OnlineTomTomNavigationFactory.Configuration(17 locationProvider: DefaultCLLocationProvider(),18 routeReplanner: RouteReplannerFactory.create(19 routePlanner: OnlineRoutePlanner(apiKey: "YOUR_API_KEY")20 ),21 apiKey: "YOUR_API_KEY"22 )2324 let navigation = try! OnlineTomTomNavigationFactory25 .create(configuration: navigationConfiguration)26 let navigationViewModel = TomTomNavigationView.ViewModel(27 navigation,28 tts: try! SystemTextToSpeechEngine()29 )3031 self.init(32 navigation: navigation,33 navigationViewModel: navigationViewModel34 )35 }3637 // MARK: Internal3839 let navigationViewModel: TomTomNavigationView.ViewModel4041 // MARK: Private4243 // ...4445 private let navigation: TomTomNavigation46 }47}
NavigationView
The initializer of a NavigationView expects a NavigationView.ViewModel
object and a closure for handling the incoming actions occurring during the navigation process.
Let’s add some methods for NavigationView Actions handling and use the previously declared navigationViewModel.
1extension ContentView {2 fileprivate func onNavigationViewAction(_ action: TomTomNavigationView.Action) {3 switch action {4 case let .arrival(action):5 onArrivalAction(action)6 case let .instruction(action):7 onInstructionAction(action)8 case let .confirmation(action):9 onConfirmationAction(action)10 case let .error(action):11 onErrorAction(action)12 }13 }1415 fileprivate func onArrivalAction(_ action: TomTomNavigationView.ArrivalAction) {16 // ...17 }1819 fileprivate func onInstructionAction(_ action: TomTomNavigationView.InstructionAction) {20 // ...21 }2223 fileprivate func onConfirmationAction(_ action: TomTomNavigationView.ConfirmationAction) {24 // ...25 }2627 fileprivate func onErrorAction(_ action: TomTomNavigationView.ErrorAction) {28 // ...29 }30}
The final result appears as follows:
1import SwiftUI23import TomTomSDKDefaultTextToSpeech4import TomTomSDKLocationProvider5import TomTomSDKNavigation6import TomTomSDKNavigationOnline7import TomTomSDKNavigationUI8import TomTomSDKRoutePlannerOnline9import TomTomSDKRouteReplannerDefault1011typealias TomTomNavigationView = TomTomSDKNavigationUI.NavigationView1213struct ContentView: View {14 // MARK: Internal1516 var body: some View {17 ZStack(alignment: .bottom) {18 // ...1920 TomTomNavigationView(21 viewModel.navigationViewModel,22 action: onNavigationViewAction(_:)23 )24 }25 }2627 // MARK: Private2829 @StateObject30 private var viewModel = ViewModel()31}
It’s worth noting that this example utilizes the @StateObject
property wrapper available starting from an iOS 14 deployment target, so if you need to support iOS 13+ you must provide the ViewModel from an outside source instead of creating it inside the SwiftUI View.
Integrating NavigationView within the UIKit codebase
The integration flow for the UIKit project is similar to the one from SwiftUI. There is a need to handle the Navigation View actions and hold the NavigationView.ViewModel
object. In order to integrate the UI component itself - the UIHostingController
is used.
1let navigationView = TomTomNavigationView(2 viewModel.navigationViewModel,3 action: onNavigationViewAction(_:)4)5let hostingController = UIHostingController(rootView: navigationView)67view.addSubview(hostingController.view)89// Add constraints10// ...
Next steps
Since you have learned about the navigation UI components, here are recommendations for the next steps: