Swift 3和iOS 10+的更新
好的,这是在Swift 3中的操作方法。基本上,有两个简单的步骤可以实现此目的:
首先,你必须修改Info.plist到列表Youtube用LSApplicationQueriesSchemes。只需将其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)
| 归档时间: |
|
| 查看次数: |
2333 次 |
| 最近记录: |