Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

launch youtube channel in youtube app

For launching video on youtube app, I am using below code.

NSURL *instagramURL = [NSURL URLWithString:@"youtube://foo"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
    NSLog(@"opening youtube app...");
    NSString *stringURL = @"http://www.youtube.com/watch?v=H9VdapQyWfg";
    NSURL *url = [NSURL URLWithString:stringURL];
    [[UIApplication sharedApplication] openURL:url];
} else {
    // open in UIWebView in WebViewViewController
    WebViewViewController *secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"webinterface"];

    secondView.headerLabel = @"YouTube";
    secondView.webPath = @"http://www.youtube.com/watch?v=H9VdapQyWfg";

    [self.navigationController pushViewController:secondView animated:YES];
}

Now client changed the mind and asking to put channel in iPhone app.

For testing, I used link http://www.youtube.com/user/richarddawkinsdotnet

BUT when I use this link, instead of youtube app, it always opens in SAFARI. :(

Any idea/ suggestion on how can I open channel in YouTube app with link provided?

like image 351
Fahim Parkar Avatar asked Dec 12 '22 11:12

Fahim Parkar


2 Answers

You're code's going wrong because, although you're checking if you can open the youTube URL more or less correctly, you're then just opening a web address, which will always open in Safari.

This is the code I've just used, which is working for me. You might want to modify the else statement if you want to fallback to using a webviewcontroller as this will open Safari if the youtube app isn't installed.

NSString *channelName = @"TheNameOfTheChannel";

NSURL *linkToAppURL = [NSURL URLWithString:[NSString stringWithFormat:@"youtube://user/%@",channelName]];
NSURL *linkToWebURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.youtube.com/user/%@",channelName]];

if ([[UIApplication sharedApplication] canOpenURL:linkToAppURL]) {
    // Can open the youtube app URL so launch the youTube app with this URL
    [[UIApplication sharedApplication] openURL:linkToAppURL];
}
else{
    // Can't open the youtube app URL so launch Safari instead
    [[UIApplication sharedApplication] openURL:linkToWebURL];
}
like image 179
Mark Bridges Avatar answered Dec 25 '22 22:12

Mark Bridges


Same answer, but shorter:

if (![[UIApplication sharedApplication] openURL: [NSURL URLWithString: @"youtube://user/%channelName%"]]) {
  NSURL *webURL = [NSURL URLWithString:@"http://www.youtube.com/user/%channelName%"];
  [[UIApplication sharedApplication] openURL: webURL];
}

and twitter:

if (![[UIApplication sharedApplication] openURL: [NSURL URLWithString: @"twitter://user?screen_name=%channelName%"]]) {
  NSURL *webURL = [NSURL URLWithString:@"http://twitter.com/%channelName%"];
  [[UIApplication sharedApplication] openURL: webURL];
}

And here is a more exhaustive list of apps:

http://wiki.akosma.com/IPhone_URL_Schemes

like image 22
Todd Horst Avatar answered Dec 26 '22 00:12

Todd Horst