问题
I have spent some days to find the Swift code for youtube Channel to open from my app. but i couldn't find At all, can some one help me please!! i need the Code in Swift.
回答1:
Update for Swift 3 and iOS 10+
OK, here is how to do it in Swift 3. Basically, there are two easy steps to achieve this:
First, you have to modify Info.plist to list Youtube with LSApplicationQueriesSchemes. Simply open Info.plist as a Source Code, and paste this:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>youtube</string>
</array>
After that, you can open any youtube URL inside Youtube application by simply substituting https:// with youtube://. Here is a complete code, you can link this code to any button you have as an Action:
@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:
I hope this suits your purpose:
var url = NSURL(string: "https://youtube.com/your-channel")
if UIApplication.sharedApplication().canOpenURL(url!) == true {
UIApplication.sharedApplication().openURL(url!)
}
来源:https://stackoverflow.com/questions/28464392/launch-youtube-channel-on-your-app-swift-code