添加CarPlay UI

sts*_*spb 6 objective-c uiscreen carplay

我正在开发我目前的iPhone音频应用程序以支持CarPlay.我已经获得Apple的批准并获得了开发权利,并观看了视频"为CarPlay启用应用程序"(https://developer.apple.com/videos/play/wwdc2017/719/).在视频中有一段Swift代码演示了如何添加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
}
Run Code Online (Sandbox Code Playgroud)

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

- (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; 
}  
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

我的问题是:我应该怎样做才能让我的应用程序通过CarPlay连接?也就是说,我应该如何获得具有UIUserInterfaceIdiomCarPlay成语的屏幕,而不仅仅是主屏幕?非常感谢提前.

小智 5

CarPlay 音频应用程序由MPPlayableContentManager控制。您需要实现MPPlayableContentDelegateMPPlayableContentDatasource协议才能连接 CarPlay。UI 由 CarPlay 控制 - 您需要做的就是为选项卡+表格(数据源)提供数据并响应可玩项目(委托)。