Basis of CarPlay framework — First Step — How to compile your first CarPlay App
Playing with basis of CarPlay.framework and Template is the goal here without spend a lot of time of research in documentation and internet!
If you want to build your first CarPlay app with Templates you are in the right place !
Let’s go to start with the initial configuration to compile a first CarPlay app step by step !
Let’s dive directly inside Xcode !
Firstly, create a new Xcode Project iOS App using Storyboard, UIKit App Delegate and Swift.
This project must adopt scenes ! (page 24/49 of the official Apple Documentation).
CarPlay Entitlement
Your project need an entitlement to select the good feature of your application (page 8 and 9/49 of the official Apple Documentation).
Here we will use com.apple.developer.carplay-audio.
Step to create entitlement :
- File > New > File > Property List > Next
- Save As CarPlayTutorial.entitlements > Create
- Update your Project > Build Settings > Code Signing Entitlements > CarPlayTutorial/CarPlayTutorial.entitlements
- Set the entitlement com.apple.developer.carplay-audio > Boolean > YES
Create Car Play Scene
- File > New > File > Swift File > CarPlaySceneDelegate
- import CarPlay
- Implements UIResponder and CPTemplateApplicationSceneDelegate
- Add a List Template
import CarPlay
class CarPlaySceneDelegate: UIResponder, CPTemplateApplicationSceneDelegate {
var interfaceController: CPInterfaceController?
func templateApplicationScene(_ templateApplicationScene: CPTemplateApplicationScene, didConnect interfaceController: CPInterfaceController) {
self.interfaceController = interfaceController
// Create a list
let item = CPListItem(text: "title", detailText: "detail")
item.accessoryType = .disclosureIndicator
let section = CPListSection(items: [item])
let listTemplate = CPListTemplate(title: "Section", sections: [section])
// Set root
self.interfaceController?.setRootTemplate(listTemplate, animated: true, completion: {_, _ in })
}
// CarPlay disconnected
private func templateApplicationScene(_ templateApplicationScene: CPTemplateApplicationScene, didDisconnect interfaceController: CPInterfaceController) {
self.interfaceController = nil
}
}
Update Application Scene Manifest
- Open Info.plist
- Application Scene Manifest >Enable Multiple Windows > YES
- Open Application Scene Manifest > Scene Configuration
- Add External Display Session Role
- Class Name > CPTemplateApplicationScene (name of the protocol implemented for CarPlay)
- Delegate Class Name > $(PRODUCT_MODULE_NAME).CarPlaySceneDelegate (CarPlay’s scene delegate)
- Configuration Name > CarPlay (unique string to identify the configuration)
- Remove Storyboard Name
Update AppDelegate
Set the connectionSceneSession depending of iOS or CarPlay.
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
if (connectingSceneSession.role == UISceneSession.Role.carTemplateApplication) {
let scene = UISceneConfiguration(name: "CarPlay", sessionRole: connectingSceneSession.role)
scene.delegateClass = CarPlaySceneDelegate.self
return scene
} else {
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
}
Run !
You can now compile your project eg. on iPhone 12 and open the CarPlay simulator !
Simulator > I/O > External Displays > CarPlay
And the result :
To go deeper…
Here you have learn how to compile a first CarPlay App using CarPlay.framework.
The code is available here tag 1.0.0 : https://github.com/T0yBoy/CarPlayTutorial/tree/1.0.0
Next tutorial will talk about how to use Templates !
It was my first article so do not hesitate if you have some questions or if you want that I go further in other part of CarPlay.framework !
Have fun !
External Documentation
https://developer.apple.com/carplay/documentation/CarPlay-App-Programming-Guide.pdf