在您的App Swift代码上启动Youtube频道

Joh*_*rry 2 youtube iphone video swift ios8

我花了几天的时间找到可以从我的应用程序打开的YouTube频道的Swift代码。但我根本找不到,请有人帮我!我需要Swift中的代码。

Ibr*_*him 7

Swift 3和iOS 10+的更新

好的,这是在Swift 3中的操作方法。基本上,有两个简单的步骤可以实现此目的:

首先,你必须修改Info.plist到列表YoutubeLSApplicationQueriesSchemes。只需将其Info.plist作为源代码打开,然后粘贴即可:

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

在这之后,你可以通过简单地替代打开里面的Youtube应用程序的任何YouTube网址https://youtube://。这是完整的代码,您可以将此代码链接为操作中具有的任何按钮:

@IBAction func YoutubeAction() {

    let YoutubeUser =  "Your Username"
    let appURL = NSURL(string: "youtube://www.youtube.com/user/\(YoutubeUser)")!
    let webURL = NSURL(string: "https://www.youtube.com/user/\(YoutubeUser)")!
    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)