Created
October 3, 2013 20:23
-
-
Save lennypham/6816549 to your computer and use it in GitHub Desktop.
iOS iFrame API - Playing a video inline in a iOS UIWebview.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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