添加CarPlay用户界面

8
我正在开发我的当前iPhone音频应用,以支持CarPlay。我已经获得了苹果公司的批准并收到了开发权限,并观看了视频“启用您的应用程序以供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
}

我将其重写为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; 
}  

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

enter image description here

我的问题是:我应该怎么做才能让我的应用程序连接到CarPlay?也就是说,我应该如何获取具有UIUserInterfaceIdiomCarPlay风格的屏幕,而不仅仅是主屏幕?非常感谢。

这篇文章和我的实验有一些更新:1. CarPlay音频应用程序不能使用上述updateCarWindow方法中基于UIScreen的方法。2. 如果我的AppDelegate符合MPPlayableContentDataSource和MPPlayableContentDelegate,并且如果我在AppDelegate.m中实现了数据源和委托方法,那么我就可以看到我的CarPlay UI。 - stspb
2个回答

5

感谢您的评论,Billy。CarPlay音频应用程序无法使用WWDC 2017视频中展示的基于UIScreen的方法。这就是为什么我的CarPlay应用程序看不到任何UI的原因。 - stspb
还有一个问题:要构建CarPlay UI,我应该在NSObject的子类中使用MPPlayableContentManager APIs(MPPlayableContentDataSource和MPPlayableContentDelegate),然后在AppDelegate.m的方法-application:didFinishLaunchingWithOptions:中实例化和初始化该NSObject子类。这样正确吗?再次感谢。 - stspb
是的 - 你可以在任何地方初始化这个类。如果你想让CarPlay对所有用户可用,application:didFinishLaunchingWithOptions是一个不错的选择。确保你的NSObject子类也是MPPlayableContentDataSourceMPPlayableContentDelegate的子类。 - Billy Caruso
遵循协议。https://www.objc.io/issues/13-architecture/subclassing/#alternative-protocols - Billy Caruso
1
谢谢,Billy。我创建了一个NSObject子类(CarPlayDemo),符合MPPlayableContentDataSource和MPPlayableContentDelegate。我在CarPlayDemo.m的-init方法中分配了MPPlayableContentManager的dataSource和delegate属性,并在CarPlayDemo.m中实现了MPPlayableContentDataSource所需的方法-numberOfChildItemsAtIndexPath:和-contentItemAtIndexPath:(但我还没有实现MPPlayableContentDelegate方法)。然而,我发现MPPlayableContentDataSource方法从未被调用。你有什么想法吗?再次感谢。 - stspb
显示剩余3条评论

2

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

最初的回答:

CarPlay导航应用程序可以使用自定义UI,但音频应用程序需要使用MediaPlayer框架的API与CarPlay配合。


1
嗨@DrMicketLaurer,我有自己的导航应用程序,它使用GLKitView(使用OpenGL ES)绘制我的应用程序并在那里显示逐步导航。我可以将其放入地图模板中吗? - iadcialim24
1
@iori24 是的,这是允许的。当/如果您获得了CarPlay地图开发的授权时,您可以这样做。 - DrMickeyLauer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接