Skip to content

Instantly share code, notes, and snippets.

@lennypham
Created October 3, 2013 20:23
Show Gist options
  • Save lennypham/6816549 to your computer and use it in GitHub Desktop.
Save lennypham/6816549 to your computer and use it in GitHub Desktop.
iOS iFrame API - Playing a video inline in a iOS UIWebview.
static NSString *youTubeVideoHTML = @"<!DOCTYPE html><html><body><div id=\"player\"><script>var tag = document.createElement('script');tag.src = \"http://www.youtube.com/iframe_api\";var firstScriptTag = document.getElementsByTagName('script')[0];firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);var player;function onYouTubeIframeAPIReady() { player = new YT.Player('player', { height: '%0.0f', width: '%0.0f', videoId: '%@', events: { 'onReady': onPlayerReady, }, playerVars: { playsinline: 1 } }); } function onPlayerReady(event) { event.target.playVideo(); }</script></body></html>";
- (void)playVideoWithId:(NSString *)videoId
{
self.myWebView.allowsInlineMediaPlayback = YES;
self.myWebView.mediaPlaybackRequiresUserAction = NO;
NSString *html = [NSString stringWithFormat:youTubeVideoHTML,
self.myWebView.frame.size.height,
self.myWebView.frame.size.width,
videoId];
[self.myWebView loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment