I am using this code to play YouTube videos on iOS
- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame
{
NSString *htmlString = [NSString stri
In Swift:
let webView = UIWebView(frame:CGRectMake(10, 10,300, 200))
webView.allowsInlineMediaPlayback = true
webView.mediaPlaybackRequiresUserAction = false
self.view.addSubview(self.webView)
let embedHTML = "<html>" +
"<body style='margin:0px;padding:0px;'>" +
"<script type='text/javascript' src='http://www.youtube.com/iframe_api'></script>"
"<script type='text/javascript'>"
"function onYouTubeIframeAPIReady()"
"{"
" ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})"
"}"
"function onPlayerReady(a)"
"{ "
" a.target.playVideo(); "
"}"
"</script>"
" <iframe id='playerId' type='text/html' width='300' height='200' src='http://www.youtube.com/embed/JW5meKfy323?enablejsapi=1&rel=0&playsinline=1&autoplay=1' frameborder='0'>"
" </body>"
"</html>"
webView.loadHTMLString(embedHTML, baseURL:NSBundle.mainBundle().resourceURL!)
If anyone is still facing this problem, below is by far the best solution I have seen. Works like a charm.
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10,300, 200)];
[self.webView setAllowsInlineMediaPlayback:YES];
[self.webView setMediaPlaybackRequiresUserAction:NO];
[self.view addSubview:self.webView];
NSString* embedHTML = [NSString stringWithFormat:@"\
<html>\
<body style='margin:0px;padding:0px;'>\
<script type='text/javascript' src='http://www.youtube.com/iframe_api'></script>\
<script type='text/javascript'>\
function onYouTubeIframeAPIReady()\
{\
ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})\
}\
function onPlayerReady(a)\
{ \
a.target.playVideo(); \
}\
</script>\
<iframe id='playerId' type='text/html' width='%d' height='%d' src='http://www.youtube.com/embed/%@?enablejsapi=1&rel=0&playsinline=1&autoplay=1' frameborder='0'>\
</body>\
</html>", 300, 200, @"JW5meKfy3fY"];
[self.webView loadHTMLString:embedHTML baseURL:[[NSBundle mainBundle] resourceURL]];
Source: https://code.google.com/p/gdata-issues/issues/detail?id=5204