Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

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>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top