我正在尝试使用 URL 方案或 YouTube.com 域从我的应用程序打开 YouTube 的应用程序,这会直接在 iOS 设备上打开 YouTube 的应用程序。
这是我试过的代码:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"youtube://results?search_query=trailer+%@",movieTitle]]];
和
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://www.youtube.com/results?search_query=trailer+%@",movieTitle]]];
但似乎没有任何效果。关于如何检索 YouTube 的 tvOS 应用程序的 URL 架构的任何想法?
最佳答案
在 Apple TV 上,您可以使用此 URL 方案在 YouTube 应用程序中播放 YouTube 视频:
youtube://watch/video_id
代码示例:
func playVideoInYouTube(_ identifier: String) {
if let url = URL(string: "youtube://watch/" + identifier),
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
else {
// Inform the user about missing YouTube app
}
}
要使用 canOpenURL
方法,您必须将方案添加到键 LSApplicationQueriesSchemes
下的 Info.plist 文件中,作为计划数组。在 XML 中,它看起来像这样:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>youtube</string>
</array>
关于objective-c - YouTube URL 方案 tvOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34473037/