I've had an app out for a while which uses UIWebView
s to display YouTube videos on certain views. Today, without any changes to the code, my users started complaining about the YouTube embeds not working: the embed with the still frame and play button displays fine, but upon clicking the play button the embed screen just goes black and nothing loads, no sound or video.
Here is my function for generating an embed code to put into the UIWebView
from a YouTube ID:
+ (NSString*)codeForYouTubeID:(NSString*)videoID width:(NSUInteger)width height:(NSUInteger)height
{
static NSString *fmt =
@"<iframe width='%d' height='%d' src='http://www.youtube.com/embed/%@?showinfo=0&modestbranding=1&rel=0&showsearch=0' frameborder='0' scrolling='0' allowfullscreen></iframe>";
return [NSString stringWithFormat:fmt, width, height, videoID];
}
Putting the resulting code into JSFiddle the embed displays and plays fine. I have also used the following alternative format string with the same results (from a StackOverflow answer here):
static NSString *fmt =
@"<object>\
<param name=\"movie\" value=\"http://www.youtube.com/v/%@\"></param>\
<embed src=\"http://www.youtube.com/v/%@\" type=\"application/x-shockwave-flash\"></embed>\
</object>";
This has the same results, but with an awkwardly auto-sized embed. The behaviour is the same: the user can click the play button, then the embed frame just goes black and nothing else happens. For me, this is happening on my test phone (iPhone 4, iOS 7) and on the simulator (both iOS6 and 7). This didn't happen before today, and no code has been changed. Has anyone else experienced this recently?
Someone found a workaround here, to an extent. Not sure how to mark an answer like that, but I'll copy it over to here for convenience.
Essentially the issue seems to relate to a change in implementation of the playsinline
parameter on YouTube's side. It doesn't matter what value you provide, or whether you use the default, but it seems to break every Youtube video embedded in this way.
The solution seems to be adding the following line before you call [myWebView loadHTMLString:string]
:
[myWebView setMediaPlaybackRequiresUserAction:NO];
I have no idea why this fixes it, but many thanks to Kyokook Hwang for providing that answer so quickly. It worked for me, and it seemed to work for many others too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With