How to Open YouTube app with YouTube id on a button click in iOS

12.4k views Asked by At

I am building an app in which i will be having a UIButton named Watch Video on clicking the button the YouTube app/web should be opened with the respective youtube ID.

I saw code to embed YouTube videos in iOS but i did not find anything of this sort. Any ideas would be really helpful.

5

There are 5 answers

3
Arpit Dongre On BEST ANSWER

This way you first check if the YouTube app is installed on a device, and then open the app or else go to Safari.

    let youtubeId = "SxTYjptEzZs"    
    var youtubeUrl = NSURL(string:"youtube://\(youtubeId)")!
    if UIApplication.sharedApplication().canOpenURL(youtubeUrl){
            UIApplication.sharedApplication().openURL(youtubeUrl)
    } else{
            youtubeUrl = NSURL(string:"https://www.youtube.com/watch?v=\(youtubeId)")!
            UIApplication.sharedApplication().openURL(youtubeUrl)
    }
0
Developer On

On button tap event write this code

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.youtube.com/watch?v=videoId"]];

This will open the video in youtube app if present ortherwise it will take user to website.

In swift

  UIApplication.sharedApplication().openURL(NSURL(string:"http://www.youtube.com/watch?v=videoId")!)
0
Sivajee Battina On

You can use URLSchemes for this purpose. We need URLSchemeIdentifier of youtube app. URLSchemeIdentifier of youtube is youtube://. For the default apps, you can get URLSchemeIdentifiers easily by searching online.

Now use below code in the button action to open youtube app.

    let url = NSURL(string:"youtube://AnyParameterYouWant")!

    if UIApplication.sharedApplication().canOpenURL(url){
            UIApplication.sharedApplication().openURL(url)
    } else{
            youtubeUrl = NSURL(string:"https://www.youtube.com/watch?v=ID")!
            UIApplication.sharedApplication().openURL(url)
    }

Note: Please note that canOpenURL will gives false in case if you won't whitelist the URL scheme. The exception is only for common apps.

0
cybergen On

Actually, you don't really need the youtube scheme. It works as expected, if you know how to switch between the two different behaviors as the user! iOS 10 let's the user choose. If an app calls openURL() with a YouTube URL the system uses the last chosen behavior.

The user can decide to use the YouTube app by clicking "Open" on the right of the top banner saying "Open in app" (or so) - the app opens and iOS remembers this setting. On the other hand, if the app is opened automatically, there appears a tiny link on the right of the status bar (yes, I actually mean the status bar!) saying "youtube.com >" (also with that chevron). By clicking that link you choose to open YouTube URLs in Safari in the future.

In other words, if the user chose to open YouTube URLs in Safari you've no chance to open the YouTube app programmatically on iOS 10.

1
Milan Nosáľ On

Swift 4 update (with removed force unwrapping):

func playInYoutube(youtubeId: String) {
    if let youtubeURL = URL(string: "youtube://\(youtubeId)"),
        UIApplication.shared.canOpenURL(youtubeURL) {
        // redirect to app
        UIApplication.shared.open(youtubeURL, options: [:], completionHandler: nil)
    } else if let youtubeURL = URL(string: "https://www.youtube.com/watch?v=\(youtubeId)") {
        // redirect through safari
        UIApplication.shared.open(youtubeURL, options: [:], completionHandler: nil)
    }
}

Don't forget to add youtube scheme to LSApplicationQueriesSchemes in Info.plist (otherwise canOpenURL will always fail):

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

Sidenote:

If the user enabled Safari app to open Youtube app before, you don't need to go through youtube:// scheme and the second url will open Youtube app, too. However, you cannot control that, and if the user declined Safari to open Youtube, then you will have to try the scheme directly anyway - therefore I believe it is better to use the solution I stated above with both conditional branches.