Carplay连接/断开连接事件?

5

有没有一种方法可以在应用程序中检测到手机是否使用Carplay连接/断开与汽车的连接? 似乎找不到任何相关的文档。 我在考虑一些系统事件,例如我可以监控。

1个回答

1
你按照这些步骤操作了吗?
  1. Add corresponding record to your project's Entitlements file: com.apple.developer.carplay-maps type of Boolean with value YES
  2. Request from Apple corresponding permission
  3. Make your AppDelegate confirm to CPApplicationDelegate protocol
  4. Implement the next methods:

    /**
     The CarPlay screen has connected and is ready to present content.
    
     Your app should create its view controller and assign it to the @c rootViewController property
     of this window.
    
     @note It is the responsibility of the delegate to maintain a reference to the interface controller beyond the scope of this method.
     */
    - (void)application:(UIApplication *)application didConnectCarInterfaceController:(CPInterfaceController *)interfaceController toWindow:(CPWindow *)window;
     /**
     The CarPlay screen has disconnected.
      */
    - (void)application:(UIApplication *)application didDisconnectCarInterfaceController:(CPInterfaceController *)interfaceController fromWindow:(CPWindow *)window;
    
请查看此文档链接2018 WWDC Carplay会议

2
请注意,这仅适用于CarPlay导航应用程序。音频应用程序不需要关心该连接状态。 - DrMickeyLauer
@DrMickeyLauer 我正在开发一个音频应用程序,您是否知道任何适用于音频应用程序的等效调用?我正在努力寻找一个好的地方来加载我的数据。 - MiMo
@MiMo 如果没有 CarPlay,你完全可以在 applicationDidFinishLaunching 中无条件地实现它,只是不会被使用。然而,如果您非常坚持这样做,一个值得的方法可能是监听音频输出路由的变化 - 这可能会在连接/断开 CarPlay 时被触发。 - DrMickeyLauer
@DrMickeyLauer 加载我的数据包括调用API、解析,我不想在每个applicationDidFinishLaunch上都这样做,因为没有CarPlay的应用程序从不需要在applicationDidFinishLaunch时获取那些数据。 - MiMo
1
@Mimo:我明白了,而且我也同意。请在苹果错误跟踪器上开一个功能愿望,CarPlay音频应用程序应该像地图应用程序一样拥有适当的(断开)回调。 - DrMickeyLauer
看起来您确实需要在授权文件中添加 com.apple.developer.carplay-maps 才能调用 CPApplicationDelegate 方法,但这的确只为地图应用程序而设计。很不幸,音频应用程序没有类似的回调函数。 - Chris Livdahl

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