iOS 使用查询打开 YouTube 应用(网址方案)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18695537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS open YouTube App with query (url schemes)
提问by 1b0t
Is there an URL Scheme to open the YouTube iOS app with a specified search query?
是否有 URL Scheme 可以使用指定的搜索查询打开 YouTube iOS 应用程序?
I tried:
我试过:
NSString *stringURL = @"http://www.youtube.com/results?search_query=foo";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
But this will open Safari instead of the YouTube Application.
但这将打开 Safari 而不是 YouTube 应用程序。
回答by Midhun MP
You can't use this http://www.youtube.com/results?search_query=foo
for opening youtube app. If you use the above url it'll open the safari instead of youtube app.
您不能使用它http://www.youtube.com/results?search_query=foo
来打开 youtube 应用程序。如果您使用上面的 url,它将打开 safari 而不是 youtube 应用程序。
There are only three URLSchemes available for opening youtube app:
只有三个 URLSchemes 可用于打开 youtube 应用程序:
http://www.youtube.com/watch?v=VIDEO_IDENTIFIER
http://www.youtube.com/v/VIDEO_IDENTIFIER
youtube://
http://www.youtube.com/watch?v=VIDEO_IDENTIFIER
http://www.youtube.com/v/VIDEO_IDENTIFIER
youtube://
Reference: iPhoneURLScheme
回答by Vaibhav Saran
this is working fine in iOS 9
这在 iOS 9 中运行良好
youtube://www.youtube.com/channel/<channel-id>
youtube://www.youtube.com/channel/<channel-id>
回答by Ibrahim
Update for Swift 3 and iOS 10+
Swift 3 和 iOS 10+ 的更新
OK, there are two easy steps to achieve this:
好的,有两个简单的步骤可以实现这一点:
First, you have to modify Info.plist
to list Youtube
with LSApplicationQueriesSchemes
. Simply open Info.plist
as a Source Code, and paste this:
首先,你必须修改Info.plist
到列表Youtube
用LSApplicationQueriesSchemes
。只需Info.plist
作为源代码打开,然后粘贴:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>youtube</string>
</array>
After that, you can open any youtube URL inside Youtube application by simply substituting https://
with youtube://
. Here is a complete code, you can link this code to any button you have as an Action:
之后,您可以通过简单地替换https://
为youtube://
. 这是一个完整的代码,您可以将此代码链接到您作为操作的任何按钮:
@IBAction func YoutubeAction() {
let YoutubeQuery = "Your Query"
let escapedYoutubeQuery = YoutubeQuery.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
let appURL = NSURL(string: "youtube://www.youtube.com/results?search_query=\(escapedYoutubeQuery!)")!
let webURL = NSURL(string: "https://www.youtube.com/results?search_query=\(escapedYoutubeQuery!)")!
let application = UIApplication.shared
if application.canOpenURL(appURL as URL) {
application.open(appURL as URL)
} else {
// if Youtube app is not installed, open URL inside Safari
application.open(webURL as URL)
}
}
回答by David Doyle
If there is a supported YouTube application search URL scheme, it isn't well documented (and will probably be fragile). You can open YouTube app itself directly using:
如果有受支持的 YouTube 应用程序搜索 URL 方案,则它没有很好的文档记录(并且可能很脆弱)。您可以直接使用以下方法打开 YouTube 应用程序:
youtube://
You can play specific videos using:
您可以使用以下方法播放特定视频:
http://www.youtube.com/watch?v=VIDEO_IDENTIFIER
I'd approach this problem by querying the list directly, and either display the results in a webview. Ideally, you don't want to replicate the YouTube app (there's already one on the app store after all), so it would be better to find the specific video identifier some other route.
我会通过直接查询列表来解决这个问题,或者在 web 视图中显示结果。理想情况下,您不想复制 YouTube 应用程序(毕竟应用程序商店中已经有一个),因此最好通过其他途径找到特定的视频标识符。
回答by Aldo Aguirre
It can be opened using
可以使用打开
vnd.youtube:///
(note three slashes are set)
(注意设置了三个斜线)
回答by Musa almatri
For iOS 11this should works:
对于iOS 11,这应该有效:
youtube://www.youtube.com/<channel-id>
回答by Jay
You can open the YouTube iOS app in this fashion:
您可以通过以下方式打开 YouTube iOS 应用:
Videos
视频
<a href="youtube://dQw4w9WgXcQ">Open in YouTube</a>
<a href="youtube://watch?v=dQw4w9WgXcQ">Open in YouTube</a>
Users and Channels
用户和渠道
Not possible at this time. (Source: extensive testing.)
目前不可能。(来源:广泛的测试。)
Playlists
播放列表
Unknown/Untested
未知/未测试
回答by Brett
If you want to open the YouTube iOS app with a search query use this syntax:
如果您想使用搜索查询打开 YouTube iOS 应用,请使用以下语法:
youtube:///results?q=search%20query
(replace search%20query
with search terms without quotes)
(替换search%20query
为不带引号的搜索词)
Note: the above syntax has three slashes instead of two.
注意:上面的语法有三个斜杠而不是两个。
(Works as of 20160420)
(作品截至 20160420)
回答by Sridhar S
According to the latest Apple Developer Documentation, youtube cannot be opened using youtube:// url scheme instead it should be as mentioned in the below link.
根据最新的 Apple 开发人员文档,youtube 不能使用 youtube:// url 方案打开,而应该在下面的链接中提到。
回答by OhioStateB
To open a Youtube link that opens in app, from a webpage (safari or any browser), and this works for opening from a javascript function too, you need to send a proper form query to Youtube:
要打开在应用程序中打开的 Youtube 链接,从网页(safari 或任何浏览器),这也适用于从 javascript 函数打开,您需要向 Youtube 发送正确的表单查询:
<form id='form' action='https://m.youtube.com/watch?v=' method='get'>
<input type='hidden' name='v' id='v' value='XgQrzkvvOO4'>
</form>
This is for mobile sites. For normal browsers change "'https://m.youtube.com/watch?v=" to "'https://youtube.com/watch?v="
这是针对移动网站的。对于普通浏览器,将 "' https://m.youtube.com/watch?v="更改为 "' https://youtube.com/watch?v="
If you need a link or button simply change the input type to button.
如果您需要链接或按钮,只需将输入类型更改为按钮。
EDIT: this method no longer works on iphone as of latest changes IOS and YT. Still searching for a method to open the Youtube APP from a script, without being asked by the browser yes or no.
编辑:从最新的 IOS 和 YT 更改开始,此方法不再适用于 iphone。仍在寻找一种从脚本中打开Youtube APP的方法,而不被浏览器询问是或否。