Friday 1 March 2013

How to play YouTube video in my iPhone app?

The solution is very simple.

UIWebView allows developers to show a web page using a simple API - loadHTMLString:baseURL:. Using it, we can also have the video embedded in our apps.

Here are steps to have the YouTube in your app.

  1. Go to YouTube and find a video you want to show in your app.
  2. In YouTube, there is a "Embedded" button under the video. Click on it to copy the embedded code.
  3. Go back to Xcode. Add the following lines to your app.
    ?
    1
    2
    3
    4
    NSString* embeddedCode = @"your embedded HTML data from YouTube"
    UIWebView* videoView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [videoView loadHTMLString:embeddedCode baseURL:nil];
    [self.window addSubview:videoView];
  4. Compile and run your app.
  5. Please note: You need to run it on a real device. Simulator does not support the YouTube framework, so you won't be able to see any video showing up in your app. It will just blank in the view.

No comments:

Post a Comment