【问题标题】:launch Youtube channel on your App Swift Code在您的 App Swift 代码上启动 Youtube 频道
【发布时间】:2015-04-12 10:00:07
【问题描述】:

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

【问题讨论】:

    标签: iphone swift video youtube ios8


    【解决方案1】:

    Swift 3 和 iOS 10+ 的更新

    好的,这里是如何在 Swift 3 中做到这一点。基本上,有两个简单的步骤可以实现:

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

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>youtube</string>
    </array>
    

    之后,您只需将https:// 替换为youtube://,即可在Youtube 应用程序中打开任何YouTube URL。这是一个完整的代码,您可以将此代码链接到您拥有的任何按钮作为操作:

    @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)
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      我希望这符合你的目的:

      var url  = NSURL(string: "https://youtube.com/your-channel")
      
      if UIApplication.sharedApplication().canOpenURL(url!) == true  {
          UIApplication.sharedApplication().openURL(url!)
      }
      

      【讨论】:

      • 亲爱的 Luca,感谢您的回复,但这是一个 webURL。我正在寻找类似于此链接“youtube.com/watch?v=5SUV1YY2yxQ”的东西,这家伙用目标 C 做的,但我正在寻找 Swift 的。
      • 我在此视频中找不到任何有关 youtube 频道的信息。您可以发布到目前为止您尝试过的内容吗?
      • NSString *channelName = @"TheNameOfTheChannel"; NSURL *linkToAppURL = [NSURL URLWithString:[NSString stringWithFormat:@"youtube://user/%@",channelName]]; NSURL *linkToWebURL = [NSURL URLWithString:[NSString stringWithFormat:@"youtube.com/user/%@",channelName]]; if ([[UIApplication sharedApplication] canOpenURL:linkToAppURL]) { // 可以打开 youtube 应用程序 URL,因此使用此 URL 启动 YouTube 应用程序 [[UIApplication sharedApplication] openURL:linkToAppURL]; } else{ // 无法打开 youtube 应用 URL 所以启动 Safari [[UIApplication sharedApplication] openURL:li
      猜你喜欢
      • 2013-08-29
      • 2016-10-30
      • 2014-06-28
      • 2018-01-27
      • 2019-08-16
      • 2014-11-14
      • 1970-01-01
      • 2011-05-27
      • 1970-01-01
      相关资源
      最近更新 更多