Adding a CarPlay UI
I am working on my current iPhone audio app that will be supported in CarPlay. I already got Apple approval, got development permission, and watched the video "Enabling the CarPlay App" ( https://developer.apple.com/videos/play/wwdc2017/719/ ). There is a Swift code snippet in the video demonstrating how to add CarPlay UI:
func updateCarWindow()
{
guard let screen = UIScreen.screens.first(where:
{ $0.traitCollection.userInterfaceIdiom == .carPlay })
else
{
// CarPlay is not connected
self.carWindow = nil;
return
}
// CarPlay is connected
let carWindow = UIWindow(frame: screen.bounds)
carWindow.screen = screen
carWindow.makeKeyAndVisible()
carWindow.rootViewController = CarViewController(nibName: nil, bundle: nil)
self.carWindow = carWindow
}
I rewrote it to the Objective-C version as shown below:
- (void) updateCarWindow
{
NSArray *screenArray = [UIScreen screens];
for (UIScreen *screen in screenArray)
{
if (screen.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiomCarPlay) // CarPlay is connected.
{
// Get the screen bounds so that you can create a window of the correct size.
CGRect screenBounds = screen.bounds;
UIWindow *tempCarWindow = [[UIWindow alloc] initWithFrame:screenBounds];
self.carWindow.screen = screen;
[self.carWindow makeKeyAndVisible];
// Set the initial UI for the window.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"VC"];
self.carWindow.rootViewController = rootViewController;
self.carWindow = tempCarWindow;
// Show the window.
self.carWindow.hidden = NO;
return;
}
}
// CarPlay is not connected.
self.carWindow = nil;
}
However, I found that the "screens" property of UIScreen always returns 1 item (main screen), regardless of whether it is tested on a real device or a simulator. So when my app is running on a simulator or real car with CarPlay, the app is just blank and says "Unable to connect to 'My app name'" (see image below). My ViewController has a simple UILabel though.
My question is, what should I do to get my app connected to CarPlay? That is, how do I get a screen that has the idiom UIUserInterfaceIdiomCarPlay and not just the main screen? Thanks a lot in advance.
source to share
CarPlay audio apps are controlled by MPPlayableContentManager . You must implement MPPlayableContentDelegate and MPPlayableContentDatasource to connect to CarPlay. The UI is controlled by CarPlay - all you have to do is feed it to tabs + tables (data source) and respond to playable items (delegate).
source to share