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

4
我正在开发一个应用程序,该应用程序将访问特定用户的YouTube上传内容。为此,我使用了Google的GData Objective-C客户端库。我使用它来获取用户的上传源并返回一个GDataEntryYouTubeVideos的NSArray。使用GDataEntryYouTubeVideo的htmlLink方法,我加载URL到一个UIWebView中,并添加了一些特殊的HTML代码。然而,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博客上这篇博客文章

如果我将创建htmlLinkString的那一行替换为以下内容:

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

然后UIWebView会正确加载视频。

我不确定我错过了什么。我是否需要做更多的事情才能将https网址加载到UIWebView中?或者GData库中是否有我遗漏的返回标准非安全URL的内容?


这对我也是一样的。但只在 iOS < 5 设备上发生。 - Jakob
我也在iOS 4上遇到了这个问题 - 还没有解决。不过iOS 5及以上的版本表现得很好。 - David Doyle
请跟随这篇文章中的信息:https://dev59.com/AnI-5IYBdhLWcg3wlpeW - 190290000 Ruble Man
1个回答

0

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

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接