我是 CarPlay 的新手,知道它与 UITableViewController 类似,但略有不同,无法使行能够被点击以运行函数/代码。网上信息不多,没有样品。苹果工程师告诉我,他们很快就会在他们的开发者网站上提供音频应用程序的样本。到目前为止,我的一切都在运行,这就是我相信我所缺少的。我有来自 Apple 的权利,在 plist 中添加了所需的内容。在实际的 CarPlay 设备中一切正常。我有两个选项卡,其中包含示例项目,一个我需要在点击时运行用于流式传输实时媒体的功能,另一个是播客列表,需要加载列表并使其可点击以为此运行功能。
这是我到目前为止一直在测试的:
protocol MPPlayableContentDelegate {
func dataReceived(data: String)
}
//Check data: indexPath.count == 2 && indexPath[0] == 0 && indexPath[1] == 0
class CarplayDataSource: NSObject, MPPlayableContentDataSource, MPPlayableContentDelegate {
let dataStore = NSObject()
var dataHelper: DataHelper!
var podcastData = [PodcastObject]()
var podcastItem = PlayerViewController()
var delegate: MPPlayableContentDelegate! = nil
func dataReceived(data: String) {
delegate?.dataReceived(data:"data received")
}
override init() {
super.init()
print("CARPLAY: init")
self.dataReceived(data: "data is now received")
}
func numberOfChildItems(at indexPath: IndexPath) -> Int {
print("CARPLAY: number of items")
if indexPath.indices.count == 0 {
print("CARPLAY: number of items > indices.count == 0 > return 2")
return 2
} else if indexPath.indices.count == 1 {
print("CARPLAY: number of items > indices.count == 1 > return 1")
return 1
}
return 1
}
func contentItem(at indexPath: IndexPath) -> MPContentItem? {
print("CARPLAY: contentItem at indexpath")
let indexCount = indexPath.count
let tabIndex = indexPath[0]
if indexCount == 1 {
if tabIndex == 0 {
let item = MPContentItem(identifier: "Tab \(tabIndex)")
item.title = "Radio"
item.isContainer = true
item.isPlayable = false
item.artwork = MPMediaItemArtwork(image: #imageLiteral(resourceName: "radio-carplay"))
return item
} else {
let item = MPContentItem(identifier: "Tab \(tabIndex)")
item.title = "Podcasts"
item.isContainer = true
item.isPlayable = false
item.artwork = MPMediaItemArtwork(image: #imageLiteral(resourceName: "recordings-carplay"))
return item
}
} else if indexCount == 2 {
if tabIndex == 0 {
let item = MPContentItem(identifier: "Tab \(tabIndex) Item")
item.title = "Listen Live" //= "Tab \(tabIndex) Item"
item.subtitle = "Radio Subtitle!"
item.isPlayable = true
item.artwork = MPMediaItemArtwork(image: #imageLiteral(resourceName: "menulogo"))
if #available(iOS 10.0, *) {
item.isStreamingContent = true
}
return item
} else {
let item = MPContentItem(identifier: "Tab \(tabIndex) Item")
item.title = "Coming Soon!" //for now since I don't now how to bring the list in
item.isPlayable = true
return item
}
}
return nil
}
func contentItem(forIdentifier identifier: String, completionHandler: @escaping (MPContentItem?, Error?) -> Swift.Void){
}
}