本文介绍了添加CarPlay UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发当前的iPhone音频应用,以在CarPlay中获得支持.我已经获得了Apple的批准,并获得了开发权利,并观看了视频为CarPlay启用您的应用"( https://developer.apple.com/videos/play/wwdc2017/719/).在视频中,有一段Swift代码演示了如何添加CarPlay UI:

I am working on my current iPhone audio app to be supported in CarPlay. I already got approved by Apple and received the development entitlement, and watched the video "Enabling Your App for CarPlay"(https://developer.apple.com/videos/play/wwdc2017/719/). In the video there is a piece of Swift code 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
}

我将其重新编写为Objective-C版本,如下所示:

I re-wrote it to an Objective-C version like following:

- (void) updateCarWindow
{
    NSArray *screenArray = [UIScreen screens];

    for (UIScreen *screen in screenArray)
    {
        if (screen.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiomCarPlay)  // CarPlay is connected.
        {
            // Get the screen's 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;
}

但是我发现,无论在真实设备或模拟器上进行测试,UIScreen的属性"screens"始终返回1个元素(主屏幕).因此,当我的应用程序在模拟器或带有CarPlay系统的真实汽车上运行时,该应用程序只是空白,并显示无法连接到我的应用程序名称""(请参见下图).我的ViewController有一个简单的UILabel.

However I found that the property "screens" of UIScreen always return 1 element (the main screen), no matter when testing on a real device or simulator. So when my app is running on the simulator or a real car with CarPlay system, the app is just blank and said "Unable to connect to "My App name"" (see the image below). My ViewController has a simple UILabel though.

我的问题是:如何使我的应用程序通过CarPlay连接?也就是说,我应该如何获得具有UIUserInterfaceIdiomCarPlay习惯用法的屏幕,而不仅是主屏幕?提前谢谢.

My question is: what should I do to make my app to be connected by CarPlay? That is, how should I obtain the screen that has UIUserInterfaceIdiomCarPlay idiom, not just always the main screen? Thanks a lot in advance.

推荐答案

CarPlay音频应用由 MPPlayableContentManager .您需要实施 MPPlayableContentDelegate MPPlayableContentDatasource 协议,以便与CarPlay连接. UI由CarPlay控制-您所需要做的就是为Tab + tables(数据源)提供数据并响应可播放项(委托).

CarPlay audio apps are controlled by the MPPlayableContentManager. You are required to implement the MPPlayableContentDelegate and MPPlayableContentDatasource protocol in order to connect with CarPlay. The UI is controlled by CarPlay - all you need to do is feed it data for tabs+tables (datasource) and respond to playable items (delegate).

这篇关于添加CarPlay UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 13:11