iOS打开带有查询的YouTube应用程序(网址方案)

1b0*_*b0t 14 youtube search ios

是否有使用指定搜索查询打开YouTube iOS应用的网址方案?

我试过了:

NSString *stringURL = @"http://www.youtube.com/results?search_query=foo";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
Run Code Online (Sandbox Code Playgroud)

但这将打开Safari而不是YouTube应用程序.

Mid*_* MP 20

你无法使用它http://www.youtube.com/results?search_query=foo来打开youtube应用程序.如果你使用上面的网址,它将打开Safari而不是youtube应用程序.

打开youtube应用只有三个URLSchemes:

  • http://www.youtube.com/watch?v=VIDEO_IDENTIFIER
  • http://www.youtube.com/v/VIDEO_IDENTIFIER
  • youtube://

参考:iPhoneURLScheme


Vai*_*ran 15

这在iOS 9中运行良好

youtube://www.youtube.com/channel/<channel-id>

  • 随着iOS 9甚至http链接,如https://www.youtube.com/watch?v=hVxPJ9heetc,将在youtube应用程序中打开,因为youtube已实现通用链接. (4认同)

Ibr*_*him 8

Swift 3和iOS 10+的更新

好的,有两个简单的步骤来实现这一目标:

首先,你必须修改Info.plist列出YoutubeLSApplicationQueriesSchemes.只需打开Info.plist源代码,然后粘贴:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>youtube</string>
</array>
Run Code Online (Sandbox Code Playgroud)

之后,您只需替换https://为,即可在Youtube应用程序中打开任何youtube URL 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)
    }

}
Run Code Online (Sandbox Code Playgroud)