Hi everyone,
I am working on a CarPlay integration, and need open a two level list opened from a root template. However, I am always receiving an error message when opening the 2nd pushed template.
To narrow it down not to have an issue with some app specific code I created the CarPlay scene below.
I am aware that there is a limit, but two levels I understand are supported on all CarPlay integrations, the maximum seems to be 5.
The app is just crashing right after showing the 2nd template.
Any ideas? Any hints are much appreciated.
Thanks a lot!
Regards, Michael
class CarPlaySceneDelegate2: UIResponder, CPTemplateApplicationSceneDelegate {
// [...]
private func createRootTemplate() -> CPListTemplate {
let pushToAItem = CPListItem(text: "Push to Template A", detailText: "Level 1 → Level 2")
pushToAItem.handler = {[weak self] (pushToAItem, completion) in
guard let self = self else {
completion()
return
}
self.interfaceController?.pushTemplate(
self.pushTemplateA(),
animated: true,
completion: { (didPresent, error) in
completion()
}
)
}
let section = CPListSection(items: [pushToAItem])
let template = CPListTemplate(title: "Root Template", sections: [section])
return template
}
private func pushTemplateA() -> CPListTemplate {
let pushToBItem = CPListItem(text: "Push to Template B", detailText: "Level 2 → Level 3")
pushToBItem.handler = {[weak self] (pushToBItem, completion) in
guard let self = self else {
completion()
return
}
self.interfaceController?.pushTemplate(
self.pushTemplateB(),
animated: true,
completion: { (didPresent, error) in
completion()
}
)
}
let backToRootItem = CPListItem(text: "Pop to Root", detailText: "Go back to root")
backToRootItem.handler = { [weak self] _, completion in
self?.interfaceController?.popToRootTemplate(animated: true, completion: nil)
completion();
}
let infoItem = CPListItem(text: "Current Depth", detailText: "2 (Template A)")
let stackCountItem = CPListItem(
text: "Stack Count",
detailText: "\((interfaceController?.templates.count ?? 0) + 1)"
)
let section = CPListSection(items: [pushToBItem, backToRootItem, infoItem, stackCountItem])
let template = CPListTemplate(title: "Template A", sections: [section])
return template
}
private func pushTemplateB() -> CPListTemplate {
let pushToCItem = CPListItem(text: "Push to Template C", detailText: "Level 3 → Level 4")
pushToCItem.handler = {[weak self] (pushToCItem, completion) in
guard let self = self else {
completion()
return
}
self.interfaceController?.pushTemplate(
self.pushTemplateC(),
animated: true,
completion: { (didPresent, error) in
completion()
}
)
}
let backToRootItem = CPListItem(text: "Pop to Root", detailText: "Go back to root")
backToRootItem.handler = { [weak self] _, completion in
self?.interfaceController?.popToRootTemplate(animated: true, completion: nil)
completion()
}
let popOneItem = CPListItem(text: "Pop One", detailText: "Go back to Template A")
popOneItem.handler = { [weak self] _, completion in
self?.interfaceController?.popTemplate(animated: true, completion: nil)
completion()
}
let infoItem = CPListItem(text: "Current Depth", detailText: "3 (Template B)")
let stackCountItem = CPListItem(
text: "Stack Count",
detailText: "\((interfaceController?.templates.count ?? 0) + 1)"
)
let section = CPListSection(items: [pushToCItem, popOneItem, backToRootItem, infoItem, stackCountItem])
let template = CPListTemplate(title: "Template B", sections: [section])
return template
}
private func pushTemplateC() -> CPListTemplate {
let backToRootItem = CPListItem(text: "Pop to Root", detailText: "Go back to root")
backToRootItem.handler = { [weak self] _, completion in
self?.interfaceController?.popToRootTemplate(animated: true, completion: nil)
completion()
}
let popOneItem = CPListItem(text: "Pop One", detailText: "Go back to Template B")
popOneItem.handler = { [weak self] _, completion in
self?.interfaceController?.popTemplate(animated: true, completion: nil)
completion()
}
let infoItem = CPListItem(text: "Current Depth", detailText: "4 (Template C)")
let stackCountItem = CPListItem(
text: "Stack Count",
detailText: "\((interfaceController?.templates.count ?? 0) + 1)"
)
let section = CPListSection(items: [popOneItem, backToRootItem, infoItem, stackCountItem])
let template = CPListTemplate(title: "Template C", sections: [section])
return template
}
}