我花了几天时间才找到用于从我的应用程序打开的 youtube channel 的 Swift 代码。但我根本找不到,请有人帮助我!! 我需要 Swift 中的代码。
最佳答案
Update for Swift 3 and iOS 10+
好的,这是在 Swift 3 中的实现方法。基本上,有两个简单的步骤可以实现:
首先,您必须修改 Info.plist
以使用 LSApplicationQueriesSchemes
列出 Youtube
。只需打开 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)
}
}
关于iphone - 在您的 App Swift 代码上启动 Youtube channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28464392/