不支持的对象 CPInformationTemplate

问题描述

我得到的错误如下。

Thread 1: "Unsupported object <CP@R_490_4045@ionTemplate: 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 = CP@R_490_4045@ionTemplate(title: "Root",layout: .leading,items: [CP@R_490_4045@ionItem(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,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")
           }

       }

}


解决方法

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

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

例如来自documentation

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