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

标签 ios youtube-api gdata-api

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

htmlLink 返回的 url 是一个 https。当我用 http 手动替换 https 时,视频按预期加载到 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代码来自这个blog post在 YouTube 的 API 博客上。

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

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

然后 UIWebView 正确加载视频。

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

最佳答案

希望结束这个老问题:

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

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

关于ios - 使用 GData 库在 UIWebView 中嵌入 YouTube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6461477/

相关文章:

iphone - 如何在 iphone sdk 中播放像 youtube 播放器这样的视频?

ios - 有效负载中的非公共(public)选择器

javascript - YouTube API 归 Google+ 页面授权 Google Apps 脚本所有

java - YouTube Java API 问题

iphone - 找不到 Google 任务 API

google-app-engine - com.google.gdata.util.ResourceNotFoundException

ios - 我怎么知道在 iOS 中是否发送了推送通知?

ios - 以编程方式设置适用于 iOS 的 Youtube YTPlayer

gdata-api - 如何通过我自己的应用程序更新谷歌应用程序标准版用户的个人资料,即使用API

iphone - 根据字符串内容更改字符串; (NSExpression 小数问题)。 Xcode