Swift 3 和 iOS 10+ 的更新
好的,有两个简单的步骤可以实现:
首先,您必须修改以Info.plist
列出. 只需作为源代码打开,然后粘贴:Youtube
LSApplicationQueriesSchemes
Info.plist
<key>LSApplicationQueriesSchemes</key>
<array>
<string>youtube</string>
</array>
之后,您可以在 Youtube 应用程序中打开任何 youtube URL,只需替换https://
为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)
}
}