IPhone SDK- Launch youtube application from within the application

iphoneuiwebviewyoutube

I am developing an app that use UIWebView to display a list of videos from youtube.
For playing the videos I use the youtube embed feature and it open the youtube application when the user clicks the video thumbnail – works great.

I want the same functionality to be executed when I click a "Watch Now" button that will be located near the youtube thumbnail on my web view but when I try to use "href" to the youtube video it opens the "youtube play page" and not the youtube iPhone application like in the embed way…

Any ideas?

Netanel.

Best Answer

I've created a test project with following code executed:

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

Running the project in Simulator allowed to reproduced the issue you've stumbled upon: test app launched Safari and it simply opened the Youtube video's page. But when I ran the same project on an iPhone the result was different: it launched the Youtube app and it started lading/playing the video in the URL right away.

The reason why the same code behaves itself differently is that there's no Youtube app in Simulator, so the only URL handler available in its environment is Safari. So everything's working as it should, you can continue developing your app.

BTW here's a really nice official blog post from Youtube about integrating it with iPhone apps: YouTube APIs + iPhone = Cool mobile apps