0

I'm trying to learn more about CarPlay. Specifically using CPTabBarTemplate (a new template as of 2020 and iOS 14).
What I'm looking to do is programmatically change the selectedTemplate, just as if I were updating a selectedTab in a UITabBar.
An example flow would be a CPTabBarTemplate with two tabs. A CPPointOfInterestTemplate and a CPInformationTemplate.

tabBarTemplate = CPTabBarTemplate(templates: [pointOfInterestTemplate, informationTemplate])

The user could select a point of interest and from the poi detail screen choose a button that might say "Select", just like in Apple's example from their WWDC20 talk. enter image description here

Upon pressing "Select", I want to programmatically take the user to the informationTemplate of the tabBarTemplate and then use the selected location's details to populate information in the informationTemplate.

I might not be understanding the way Apple expects CPTabBarTemplate to be used. There does exist a selectedTab property of CPTabBarTemplate. However, that property is get only. Which leads me to believe that the desired way to switch tabs is let the user tap the next tab themselves.

I'd be grateful for any insight. If anyone knows how to programmatically update the selected tab of a CPTabBarTemplate that would be dandy.

Thanks for reading!

Xeaza
  • 1,410
  • 1
  • 17
  • 21

2 Answers2

1

I found a hacky solution for this case. Dirty code, but works for me:

root.updateTemplates([destinationTemplate])
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
    root.updateTemplates(allTemplates)
}

The idea is to set your destination template as the only template. In this case the system automatically switches to it, as it is the only one. Then after a short delay you update your tab bar template to display all the templates. After doing it the system persists your selection.

0

I don't think this is currently possible. The public interface of CPTabBarTemplate is very short but I couldn't even get it working by calling private api. Neither calling tabTemplate.perform(Selector(("setIndexOfSelectedTab:")), with: 1) nor the invoking function tabTemplate.perform(Selector(("handleActionForControlIdentifier:")), with: tabTemplates[1].value(forKey: "identifier")) made the trick (even though the tab bar delegate function is called with the correct selected template).

fruitcoder
  • 1,073
  • 8
  • 24
  • Turns out, you are correct. According to Apple `It is not currently possible to programmatically select a tab without user action.` https://developer.apple.com/forums/thread/681878 – Xeaza Aug 09 '21 at 15:38