9

I have webview displaying youtube video:

class ViewController: UIViewController {

@IBOutlet var webView: UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    let url = NSURL(string: "http://www.cast.html")
    let request = NSURLRequest(URL: url!)

    webView.loadRequest(request)
}

HTML link looks like this:

<div class="h_iframe">
<iframe webkit-playsinline height="480" width="2" src="https://www.youtube.com/embed/ru1_lI84Wkw?feature=player_detailpage&playsinline=1" frameborder="0" allowfullscreen></iframe></div>

That works perfectly, but i want also to let user to watch it on the youtube app. Is it possible to create link in webview that launch YouTube app (if installed on device) ?

Any help appreciated.

Jay
  • 83
  • 1
  • 1
  • 8
  • possible duplicate of [iOS open YouTube App with query (url schemes)](http://stackoverflow.com/questions/18695537/ios-open-youtube-app-with-query-url-schemes) – Anthony Kong Nov 17 '14 at 08:57

4 Answers4

27

Since Youtube isn't pre-installed on the phone, it's a good idea to safeguard this by testing the URL then falling back to using safari if they don't have youtube installed.

Add this key to your info.plist

<key>LSApplicationQueriesSchemes</key>
<array>
   <string>youtube</string>
</array>

Then this is the code that will fallback to safari if the Youtube app isn't installed.

    let youtubeId = "vklj235nlw"
    var url = URL(string:"youtube://\(youtubeId)")!
    if !UIApplication.shared.canOpenURL(url)  {
        url = URL(string:"http://www.youtube.com/watch?v=\(youtubeId)")!
    }
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
William T.
  • 12,831
  • 4
  • 56
  • 53
  • 3
    As for iOS 9, you also need the `LSApplicationQueriesSchemes` set in the `info.plist` to an array containing `youtube` http://stackoverflow.com/questions/40119595/canopenurl-not-working-in-ios-10 – Teodor Ciuraru Jan 17 '17 at 09:25
16

You can use this:

UIApplication.sharedApplication().openURL("youtube://XXXXXX")

where XXXXXX is the code of the video in youtube.

Gabriel
  • 3,319
  • 1
  • 16
  • 21
  • Thanks. This helps. But how do I come back to my app. Is there any provision like back button? – ABM May 03 '21 at 10:44
14

Update for Swift 3 and iOS 10+

OK, 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 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)
    }

}
Ibrahim
  • 6,006
  • 3
  • 39
  • 50
1

Swift 3

UIApplication.shared.openURL(URL(string: "http://youtube.com")!)

if YouTube app is not installed, it opens Safari (web page)