Fahim Parkar
Fahim Parkar

Reputation: 31647

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?

Upvotes: 4

Views: 4466

Answers (3)

Vaibhav Saran
Vaibhav Saran

Reputation: 12908

youtube://user/<channel-id> stopped working in iOS 9 so i research and found this working well now in iOS 9

youtube://www.youtube.com/channel/<channel-id>

Upvotes: 0

Todd Horst
Todd Horst

Reputation: 863

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

Upvotes: 1

Mark Bridges
Mark Bridges

Reputation: 8448

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];
}

Upvotes: 12

Related Questions