我花了几天时间找到从我的应用程序打开 youtube 频道的 Swift 代码。但我根本找不到,有人可以帮助我吗?
我需要 Swift 中的代码。
Swift 3 和 iOS 10+ 的更新
好的,下面是如何在 Swift 3 中做到这一点。基本上,有两个简单的步骤可以实现这一点:
首先,你必须修改Info.plist
列出Youtube
with LSApplicationQueriesSchemes
。只需打开Info.plist
作为源代码,然后粘贴:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>youtube</string>
</array>
之后,您可以通过简单地替换来打开 Youtube 应用程序内的任何 YouTube URLhttps://
with 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)
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)