【问题标题】:Embedding YouTube Video in UIWebView Using GData Library使用 GData 库在 UIWebView 中嵌入 YouTube 视频
【发布时间】:2011-09-21 15:12:21
【问题描述】:

我正在构建一个可以访问特定用户的 YouTube 上传内容的应用。为此,我使用了 Google 的 GData Objective-C 客户端库。我使用它来访问用户的上传提要并返回 GDataEntryYouTubeVideos 的 NSArray。使用 GDataEntryYouTubeVideo 的 htmlLink 方法,我使用一些特殊的 HTML 代码将 url 加载到 UIWebView 中。但是,UIWebView 只是显示一个红色页面。

htmlLink 返回的 url 是 https。当我手动将 https 替换为 http 时,视频会按预期加载到 UIWebView 中。以下代码不会加载 YouTube 视频:

- (void)viewDidLoad
{
    [super viewDidLoad];

    GDataServiceGoogleYouTube *youtubeService = [[GDataServiceGoogleYouTube alloc] init];
    NSURL *uploadUrl = [GDataServiceGoogleYouTube youTubeURLForUserID:@"higaara" userFeedID:kGDataYouTubeUserFeedIDUploads];
    void(^handler)(GDataServiceTicket *ticket, GDataFeedBase *feed, NSError *error) = ^(GDataServiceTicket *ticket, GDataFeedBase *feed, NSError *error)
    {
        NSLog(@"handler");
        // Get the link from the youtube entry
        GDataEntryYouTubeVideo *youTubeVideo = [feed firstEntry];

        NSString *htmlLinkString = [[youTubeVideo HTMLLink] href];

        // Create an html string to load into the web view
        NSString *htmlString = [NSString stringWithFormat:@"<html><head><meta name =
        \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width =
        212\"/></head><body style=\"background:#F00;margin-top:0px;margin-
        left:0px\"><div><object width=\"212\" height=\"172\"><param name=\"movie\"
        value=\"%@\"></param><param name=\"wmode\" value=\"transparent\"></param><embed
        src=\"%@\"type=\"application/x-shockwave-flash\" wmode=\"transparent\"
        width=\"212\" height=\"172\"></embed></object></div></body></html>",
        htmlLinkString, htmlLinkString];
        NSURL *tempurl = [NSURL URLWithString:@"http://onnati.net/apptrap"];
        [webView loadHTMLString:htmlString tempurl];
    };

    [youtubeService fetchFeedWithURL:uploadUrl completionHandler:handler];
}

htmlString 中的 html 代码来自 YouTube 的 API 博客上的 blog post

如果我用这个替换 htmlLinkString 创建行:

NSString *htmlLinkString = [[[youTubeVideo HTMLLink] href] stringByReplacingOccurrencesOfString:@"https://" withString:@"http://"];

然后 UIWebView 正确加载视频。

我不确定我在这里缺少什么。 我需要做更多的事情才能将 https url 加载到 UIWebView 中吗? 或者我在 GData 库中缺少返回标准非安全 URL 的东西?

【问题讨论】:

  • 这也发生在我身上。但仅适用于 iOS
  • 我在 iOS 4 上也遇到了这个问题 - 还没有解决。不过,iOS 5 及更高版本运行良好。
  • 关注这篇文章的信息:stackoverflow.com/questions/1779511/…

标签: ios youtube-api gdata-api


【解决方案1】:

希望结束这个老问题:

一些旧版本的移动版 Safari 会自动将嵌入的 YouTube Flash 翻译成可点击的缩略图,这些缩略图将使用另一种播放机制,因为 iOS 显然不支持 Flash。将这些嵌入内容转换为可点击链接的逻辑可能有一个硬编码模式,用于检查 http:// 而不是 https:// URL。

在 iOS UIWebView 中嵌入 YouTube 视频的推荐方法是使用 iframe embed。您可以从 Data API 获取视频 ID,然后使用它来构建 iframe URL。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-11
    • 2013-01-19
    • 1970-01-01
    • 2013-06-14
    • 2012-08-08
    • 1970-01-01
    • 2014-04-02
    • 2012-12-09
    相关资源
    最近更新 更多