Issue

Embedding Youtube Video in iOS app does not diplay the Youtube Video, instead there is any error display in the logs.

Error

*** WebKit discarding exception: <NSRangeException> *** -[__NSCFString substringToIndex:]: Range or index out of bounds

Here is how the YouTube video url is created:

NSString *url = [[NSString alloc] initWithFormat:@"http://www.youtube.com/watch?v=%@&f=gdata_videos",@"XYZ"];

And here is the embeded HTML for the same for the UIWebView.

            NSString *embedYouTube = [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="320" height="480"><param name="movie" value="%@"></param><param name="wmode" value="transparent"></param><embed src="/public/wordpress/x-shockwave-flash" wmode="transparent" width="320" height="480"></embed></object></div></body></html>",url,url]    ;

<!>

Solution

The issue is the Youtube Video URL pattern with watch?v=xyz   does not work and it should be replaced with v/xyz where xyz is the Youtube video id. It should be as:

NSString *url = [[NSStringalloc] initWithFormat:@"http://www.youtube.com/v/%@",@"XYZ"]];