使用GData库将YouTube视频嵌入UIWebView

问题描述

| 我正在构建一个可以访问特定用户的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,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 URL加载到UIWebView中,是否需要做更多的事情?还是GData库中缺少我返回标准非安全URL的东西?     

解决方法

        希望结束这个老问题: 一些旧版的移动Safari浏览器会自动将YouTube Flash嵌入到可点击的缩略图中,从而使用其他播放机制,因为iOS显然不支持Flash。将这些嵌入转换为可点击链接的逻辑可能具有硬编码模式,该模式检查“ 2”个URL,而不是“ 3”个URL。 将YouTube视频嵌入iOS UIWebView的推荐方法是使用iframe嵌入。您可以从Data API获取视频ID,然后使用该视频ID构造iframe URL。