4

I'm building an app that will access a specific user's YouTube uploads. For this, I'm using Google's GData Objective-C client library. I use it to get access to the user's uploads feed and return an NSArray of GDataEntryYouTubeVideos. Using GDataEntryYouTubeVideo's htmlLink method, I load the url into a UIWebView with some special HTML code. However, the UIWebView just shows a red page.

The url that htmlLink returns is an https. When I manually replace https with http, the video loads in the UIWebView as expected. The following code does not load the YouTube video:

- (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];
}

The html code in htmlString is from this blog post on YouTube's API blog.

If I replace the htmlLinkString creation line with this:

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

then the UIWebView properly loads the video.

I'm not sure what I'm missing here. Do I need to do something more in order to load an https url into a UIWebView? Or is there something I'm missing in the GData library that returns a standard non-secure URL?

Kumaran
  • 166
  • 1
  • 9

1 Answers1

0

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 not https:// 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.

Jeff Posnick
  • 45,379
  • 12
  • 113
  • 142