CarPlay 的 setRootTemplate 无法运行完成处理程序或使用 async/await 工作



使用 CarPlay 的setRootTemplate方法时,我无法让完成处理程序async/await 正常工作。我需要在加载根模板后执行一些操作,但这两个选项都不允许这样做。

此方法记录在这里,说:

CarPlay 在显示模板后调用完成。当演示成功时,布尔参数为 true;否则,它是假的,CarPlay 会提供一个描述故障的错误。CarPlay 会引发异常,如果演示文稿失败,并且您未提供闭包。

示例 1:完成处理程序

interfaceController.setRootTemplate(tabBarTemplate, animated: false) { didSucceed, error in
print("This completion handler never runs...")
}

注意:如果将nil传递给完成参数,上述方法确实有效。

示例 2:异步/等待

do {
print("This appears in console...")
try await interfaceController.setRootTemplate(tabBarTemplate, animated: true)
print("This doesn't...")
} catch {
print("error (error.localizedDescription)")
}

此方法的同步版本确实有效,但它在 iOS 15 中会引发弃用警告。

这是一个错误,还是我缺少一些明显的东西?

根据苹果开发人员文档setRootTemplate有 3 个参数。

我在您的代码中发现了错误的语法。

使用completion参数

如下
self.interfaceController?.setRootTemplate(listTemplate, animated: true, completion: { success, error in
print("onCompletion")
print(success)
print(error)
})

希望对...

最新更新