问题描述
根据Apple's Documentation,应该显示一个@L_502_1@ 来表示某些内容正在加载或需要时间在屏幕上显示。这是合理的,我很乐意这样做...
我觉得我遗漏了一些明显的东西,但我无法弄清楚如何在 CarPlay 应用程序的模板中显示活动指示器,因为我无权访问该视图。我只能与可用的模板交互。
如果我试图匹配 Apple 文档中的图像,我会假设我需要使用 listTemplate
以及如何向其中添加活动指示器。
有没有其他人遇到过这个?任何帮助将不胜感激!
解决方法
我遇到了与您相同的问题 - 我无法找到如何在从 API 异步加载内容时显示加载微调器。 Apple Docs 在试图解决这个问题时并不是很有帮助。经过大量测试,尤其是在汽车中的真实 CarPlay 设备上,我有了答案。
对我来说,在 MPPlayableContentDataSource 的 beginLoadingChildItems() 中请求数据时,如果我几秒钟内没有调用 beginLoadingChildItems() 的完成处理程序,CarPlay 屏幕上就会显示一个加载微调器。所以beginLoadingChildItems() 被CarPlay 调用,我发出一个API 请求,只有当请求返回数据(或失败)时,我才调用beginLoadingChildItems() 的完成处理程序。大多数情况下,API 请求返回得非常快,加载微调器不显示。但是当 API 很慢时(由于连接不稳定导致几秒钟),加载微调器会显示。
我的 MPPlayableContentDataSource beginLoadingChildItems() 看起来像:
class CarPlayContentManager: NSObject,MPPlayableContentDataSource {
private let contentProvider = ContentProvider()
func beginLoadingChildItems(at indexPath: IndexPath,completionHandler: @escaping (Error?) -> Void) {
// Make a request to fetch data from the API,passing in completionHandler
contentProvider.fetchContentItems(completionHandler: completionHandler)
}
}
ContentProvider 只是一个获取数据的辅助类:
class ContentProvider {
func fetchContentItems(completionHandler: @escaping (Error?) -> Void) {
// Make the async API request
// When the API request returns (either as success or error) call completionHandler(nil)
}
}
注意:您可以通过在 beginLoadingChildItems() 中设置延迟来测试加载微调器。调用 beginLoadingChildItems() 时,只需设置 5 秒的延迟,然后在 5 秒结束后调用 completionHandler(nil)。这应该会在 CarPlay 屏幕上显示加载微调器。