使用 GData 库在 UIWebView 中嵌入 YouTube 视频

发布于 2024-11-17 04:47:11 字数 2482 浏览 7 评论 0 原文

我正在构建一个可以访问特定用户的 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 代码来自此 博客文章

如果我用以下内容替换 htmlLinkString 创建行:

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

那么 UIWebView 会正确加载视频。

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

I'm building an app that will access a specific user's YouTube uploads. For this, I'm using Google's GData Objective-C client library. I use it to get access to the user's uploads feed and return an NSArray of GDataEntryYouTubeVideos. Using GDataEntryYouTubeVideo's htmlLink method, I load the url into a UIWebView with some special HTML code. However, the UIWebView just shows a red page.

The url that htmlLink returns is an https. When I manually replace https with http, the video loads in the UIWebView as expected. The following code does not load the YouTube video:

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

The html code in htmlString is from this blog post on YouTube's API blog.

If I replace the htmlLinkString creation line with this:

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

then the UIWebView properly loads the video.

I'm not sure what I'm missing here. Do I need to do something more in order to load an https url into a UIWebView? Or is there something I'm missing in the GData library that returns a standard non-secure URL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说好的呢 2024-11-24 04:47:11

希望能够解决这个老问题:

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

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

To hopefully close out this old question:

Some older versions of mobile Safari automatically translated YouTube Flash embeds into clickable thumbnails that would use an alternative playback mechanism, since iOS obviously doesn't support Flash. The logic to turn those embeds into clickable links probably has a hardcoded pattern that checks for http:// and not https:// URLs.

The recommended way to embed YouTube videos within an iOS UIWebView is using the iframe embed. You can obtain the video id from the Data API and then use that to construct the iframe URL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文