Hello, I'm somewhat new to CarPlay integration and am having an issue. I have ready through Apple's CarPlay Programming Guide, reviewed their code samples and have exhausted my searches online to help find a solution to my problem.
I have been unable to get a basic map to display on my CarPlay map utilizing the following:
import CarPlay
class CarPlaySceneDelegate: UIResponder, CPTemplateApplicationSceneDelegate { var interfaceController: CPInterfaceController? var window: CPWindow?
func templateApplicationScene(_ templateApplicationScene: CPTemplateApplicationScene, didConnect interfaceController: CPInterfaceController) {
self.interfaceController = interfaceController
let mapTemplate = CPMapTemplate()
mapTemplate.mapDelegate = self
interfaceController.setRootTemplate(mapTemplate, animated: true, completion: { success, error in
if let error = error {
debugPrint("Error: \(error)")
} else {
print("CarPlay Map Should Be Displayed")
}
})
let trip = CPTrip(origin: MKMapItem(placemark: .init(coordinate: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0))), destination: MKMapItem(placemark: .init(coordinate: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0))), routeChoices: [])
mapTemplate.startNavigationSession(for: trip)
}
}
extension CarPlaySceneDelegate: CPMapTemplateDelegate { func mapTemplate(_ mapTemplate: CPMapTemplate, panWith direction: CPMapTemplate.PanDirection) { // Handle panning }
func mapTemplate(_ mapTemplate: CPMapTemplate, startedTrip trip: CPTrip, using routeChoice: CPRouteChoice) {
// Handle trip start
}
}
I have my CarPlay Entitlements setup, I have my CarPlay Navigation App set in my signing and capabilities and my app icon displays properly on CarPlay (both in simulator and inside of my vehicle). However, as mentioned I only get a white screen.
Now, if I utilize the following code, I will get my map to display, however I lose functionality such as panning the map. I'm sure that I am missing something simple on the above example and appreciate any guidance that you may have.
func createMapTemplate(destination: TripDetails?, destinationBL: BucketListItems?, route: MKRoute, window: UIWindow) -> CPMapTemplate {
mapTemplate = CPMapTemplate()
mapTemplate.mapDelegate = self
trip = nil
let startLocation = CLLocation(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let startMapItem = MKMapItem(placemark: MKPlacemark(coordinate: startLocation.coordinate))
startMapItem.name = "Starting Location"
let endMapItem = MKMapItem(placemark: MKPlacemark(coordinate: route.polyline.points()[route.polyline.pointCount - 1].coordinate))
endMapItem.name = destination?.campgroundName != nil ? destination!.campgroundName : destinationBL!.name
// Create the hosting controller for the SwiftUI view
let mapViewController = UIHostingController(rootView: CarPlayMapView(templateManager: self))
window.rootViewController = mapViewController
window.makeKeyAndVisible()
let routeChoice = createCPRouteChoice(from: route)
trip = CPTrip(origin: startMapItem, destination: endMapItem, routeChoices: [routeChoice])
mapTemplate(mapTemplate, selectedPreviewFor: trip!, using: routeChoice)
mapTemplate.showTripPreviews([trip!], textConfiguration: nil)
return mapTemplate
}