Update for Swift 3 and iOS 10+
OK, there are two simple steps for this:
First you need to change Info.plist to a Youtube list on LSApplicationQueriesSchemes . Just open Info.plist as source code and paste this:
<key>LSApplicationQueriesSchemes</key> <array> <string>youtube</string> </array>
After that, you can open any Youtube URL in the Youtube application by simply replacing https:// with youtube:// . Here is the complete code, you can associate this code with any button that you use as an action:
@IBAction func YoutubeAction() { let YoutubeID = "Ktync4j_nmA" // Your Youtube ID here let appURL = NSURL(string: "youtube://www.youtube.com/watch?v=\(YoutubeID)")! let webURL = NSURL(string: "https://www.youtube.com/watch?v=\(YoutubeID)")! 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) } }
source share