Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YouTube URL Scheme tvOS

I am trying to open YouTube's app from my application with the URL scheme or the YouTube.com domain which opens YouTube's app directly on an iOS device.

This is the code I tried:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"youtube://results?search_query=trailer+%@",movieTitle]]];

and

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://www.youtube.com/results?search_query=trailer+%@",movieTitle]]];

But nothing seems to work. Any ideas on how to retrieve the URL scheme for YouTube's tvOS application?

like image 939
BlackM Avatar asked Dec 26 '15 16:12

BlackM


1 Answers

On Apple TV you can use this URL scheme to play a YouTube video in the YouTube app:

youtube://watch/video_id

Code example:

func playVideoInYouTube(_ identifier: String) {

    if let url = URL(string: "youtube://watch/" + identifier),
        UIApplication.shared.canOpenURL(url) {

        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }
    else {
        // Inform the user about missing YouTube app
    }
}

To use the canOpenURL method, you have to add the scheme to the Info.plist file under key LSApplicationQueriesSchemes, as part of a schemes array. In XML it looks like this:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>youtube</string>
</array>
like image 172
Ely Avatar answered Oct 23 '22 02:10

Ely