【问题标题】:launch youtube channel in youtube app在 youtube 应用中启动 youtube 频道
【发布时间】:2013-08-29 07:15:15
【问题描述】:

为了在 youtube 应用上启动视频,我使用以下代码。

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

现在客户改变主意,要求将频道放入 iPhone 应用程序中。

为了测试,我使用了链接http://www.youtube.com/user/richarddawkinsdotnet

但是当我使用此链接时它总是在 SAFARI 中打开,而不是 youtube 应用。 :(

关于如何在 YouTube 应用中打开频道并提供链接的任何想法/建议?

【问题讨论】:

    标签: ios objective-c youtube youtube-channels


    【解决方案1】:

    您的代码出错了,因为尽管您正在检查是否可以或多或少正确地打开 YouTube URL,但您只是打开了一个网址,该网址将始终在 Safari 中打开。

    这是我刚刚使用的代码,它对我有用。如果您想回退到使用 webviewcontroller,您可能需要修改 else 语句,因为如果未安装 youtube 应用程序,这将打开 Safari。

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

    【讨论】:

    • 我会检查这个并回复你...我从来没有找到这个答案..希望这有效.. .:D :P
    • 这对我不起作用!它只是进入应用程序的视频页面并显示“播放错误点击重试”但在 safari 中打开时工作正常。
    • 如果你想在原生应用中打开 URL,你应该这样使用:NSURL *linkToAppURL = @"youtube://www.youtube.com/user/%@",channelName]; .....以下方案将不起作用 NSURL *linkToAppURL = @"youtube://user/%@",channelName];
    • 有没有办法打开特定的视频?
    【解决方案2】:

    相同的答案,但更短:

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

    和推特:

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

    以下是更详尽的应用列表:

    http://wiki.akosma.com/IPhone_URL_Schemes

    【讨论】:

      【解决方案3】:

      youtube://user/<channel-id> 停止在 iOS 9 中工作,所以我研究并发现它现在在 iOS 9 中运行良好

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

      【讨论】:

        猜你喜欢
        • 2016-10-30
        • 2013-05-06
        • 2012-03-15
        • 2015-07-12
        • 2017-07-02
        • 1970-01-01
        • 2015-04-12
        • 2011-05-27
        • 2015-07-07
        相关资源
        最近更新 更多