我正在构建一个可以访问特定用户上传的 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/