使用 GData 库在 UIWebView 中嵌入 YouTube 视频
我正在构建一个可以访问特定用户的 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 的内容?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
希望能够解决这个老问题:
一些旧版本的移动 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 nothttps://
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.