不支持的对象 CPInformationTemplate

时间:2021-04-24 05:31:08

标签: swift xcode carplay

我得到的错误如下。

Thread 1: "Unsupported object <CPInformationTemplate: 0x6000012de010> <identifier: 3444D3F1-ECFF-4953-B543-459286E11371, userInfo: (null), tabTitle: (null), tabImage: (null), showsTabBadge: 0> passed to setRootTemplate:animated:completion:. Allowed classes: {(\n    CPTabBarTemplate,\n    CPListTemplate,\n    CPGridTemplate,\n    CPAlertTemplate,\n    CPVoiceControlTemplate,\n    CPNowPlayingTemplate\n)}"

我所做的只是显示一个简单的基本文本。 - 我正在关注https://adapptor.com.au/blog/enhance-existing-apps-with-carplay

任何建议都会有所帮助。

import Foundation
import CarPlay

class CarPlaySceneDelegate: UIResponder, CPTemplateApplicationSceneDelegate {
    
  func templateApplicationScene(_ templateApplicationScene: CPTemplateApplicationScene,
                                  didConnect interfaceController: CPInterfaceController) {
    
    if #available(iOS 14.0, *) {
        let screen = CPInformationTemplate(title: "Root", layout: .leading, items: [CPInformationItem(title: "Hello", detail: "CarPlay")], actions: [])
 
        interfaceController.setRootTemplate(screen, animated: true, completion: { _,_ in
            // Do nothing
        })
      } else {
          // Fallback on earlier versions
      }
      
  }
}

我的 AppDelegate 是:

//
//  AppDelegate.swift
//  vscroll
//
//  Created by Russell Harrower on 17/8/20.
//  Copyright © 2020 Russell Harrower. All rights reserved.
//

import UIKit
import Flurry_iOS_SDK
import AVKit


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        MusicPlayer.shared.startBackgroundMusic(url:"https://api.drn1.com.au:9000/station/DRN1", type: "radio")
        setupNotifications()
        Flurry.startSession("GJV665GWWF25GPCD25W8", with: FlurrySessionBuilder
              .init()
              .withCrashReporting(true)
              .withLogLevel(FlurryLogLevelAll))
        return true
    }
    
    
    
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {

      if(connectingSceneSession.role == UISceneSession.Role.carTemplateApplication) {
        let scene =  UISceneConfiguration(name: "CarPlay", sessionRole: connectingSceneSession.role)

        // At the time of writing this blog post there seems to be a bug with the info.plist file where
        // the delegateClass isn't set correctly. So we manually set it here.
        if #available(iOS 14.0, *) {
            scene.delegateClass = CarPlaySceneDelegate.self
        } else {
            // Fallback on earlier versions
        }
                
        return scene
       } else {
         let scene =  UISceneConfiguration(name: "Phone", sessionRole: connectingSceneSession.role)
                
          return scene
       }
    }

    // MARK: UISceneSession Lifecycle

  /*  func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }*/

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }
    
    
    //CUSTOM CODE
    
    func setupNotifications() {
           // Get the default notification center instance.
           let nc = NotificationCenter.default
           nc.addObserver(self,
                          selector: #selector(handleInterruption),
                          name: AVAudioSession.interruptionNotification,
                          object: nil)
       }
       
       @objc func handleInterruption(notification: Notification) {

           guard let userInfo = notification.userInfo,
               let interruptionTypeRawValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,
               let interruptionType = AVAudioSession.InterruptionType(rawValue: interruptionTypeRawValue) else {
               return
           }

           switch interruptionType {
           case .began:
               print("interruption began")
           case .ended:
            MusicPlayer.shared.player?.play()
               print("interruption ended")
           default:
               print("UNKNOWN")
           }

       }

}


1 个答案:

答案 0 :(得分:1)

您在项目中定义了哪些权利?模板的可用性取决于定义的应用权利。

使用不受支持的模板 (CPInformationTemplate) 会导致您的错误。

例如来自documentation

<块引用>

您不能在带有音频的应用中使用 CPInformationTemplate 权利。

相关问题