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

5

6

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.

Sickly answered 9/1, 2017 at 6:48 Comment(1)
#18696037Bale
M
13

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)
    }
Meanly answered 9/1, 2017 at 6:58 Comment(3)
Do you have YouTube App installed in device?Meanly
try doing the first step in this answerMeanly
I posted an answer because my findings could be helpful for others.Pazpaza
A
12

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.

Alford answered 25/1, 2018 at 9:52 Comment(1)
can be simplified to UIApplication.shared.open(youtubeURL) since the other parameters have default valuesRadiography
H
2

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")!)
Heartsease answered 9/1, 2017 at 7:1 Comment(0)
P
2

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.

Pazpaza answered 26/7, 2017 at 18:10 Comment(0)
F
1

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.

Flannel answered 26/7, 2017 at 18:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.