首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加CarPlay UI

添加CarPlay UI
EN

Stack Overflow用户
提问于 2017-07-25 19:01:06
回答 2查看 4.3K关注 0票数 8

我正在开发我目前在CarPlay中支持的iPhone音频应用程序。我已经得到了苹果公司的批准,获得了开发授权,并观看了视频“为你的应用程序启用CarPlay"(https://developer.apple.com/videos/play/wwdc2017/719/)。在视频中,有一段Swift代码演示了如何添加CarPlay UI:

代码语言:javascript
复制
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版本,如下所示:

代码语言:javascript
复制
- (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的“屏幕”属性总是返回1个元素(主屏幕),无论是在真实设备上测试还是在模拟器上测试。因此,当我的应用程序在模拟器上运行或在装有CarPlay系统的真实汽车上运行时,该应用程序就是空白的,并显示“无法连接到”我的应用程序名称“”(见下图)。不过,我的ViewController有一个简单的UILabel。

我的问题是:我应该怎么做才能让我的应用程序通过CarPlay连接?也就是说,我应该如何获得带有UIUserInterfaceIdiomCarPlay习惯用法的屏幕,而不仅仅是主屏幕?在此之前非常感谢。

EN

回答 2

Stack Overflow用户

发布于 2017-07-28 16:12:26

CarPlay音频应用程序由MPPlayableContentManager控制。您需要实现MPPlayableContentDelegateMPPlayableContentDatasource协议才能与CarPlay连接。UI是由CarPlay控制的-你所需要做的就是为它提供tabs+tables (数据源)的数据,并响应可播放的项目(委托)。

票数 5
EN

Stack Overflow用户

发布于 2019-02-06 00:33:47

自定义UI仅适用于CarPlay导航应用程序。对于音频应用程序,MediaPlayer框架包含了使用CarPlay所需的所有接口。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45301362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档