13 Apr 2014

iPhone Development: Play YouTube video in UIWebview

Video a more easiest way to tell your users about your app behavior rather then providing them textual information about the app. Now days its a most common feature used by many app developers to place  video in information section of their app. Some of the developers used local video files and some of them want remote file hosted on remote server so that they can change that file without updating their app.

In this post,  i am going to tell you how we can play YouTube video in our iPhone app. As you all know since native media player does not support flash files and thus we are not able to play YouTube video in native media player. Though, we can play YouTube video in UIWebview. But the problem is that if we direct pass link of our YouTube video URL then it displays whole page with video player rather then video player only. To overcome this, Google provides i frame that can be embedded into our webpage.
                      We will use UIWebview method, - (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL to load our i frame string.

To create i frame we will use below code

Link to download File IFrame Youtube Code

As we have our i frame that will construct a video control for us, it time to load  are embedString in UIWebview.

 [self.webView loadHTMLString:html baseURL:nil];

You will see your video getting loaded with nice video player and white frame border around it. Tap on video and it will start playing.

To stop video from continuously playing when user  tap on back button without pressing pause load your UIWebview with blank URL.


No comments:

Post a Comment