Iphone – Problem embedding an youtube video on iphone

iframeiphoneuiwebviewyoutube

I am trying to embed a video with the following code to avoid launching the youtube app and stay in my app:

-(void)embedYouTube:(NSString*)url frame:(CGRect)frame {  

 NSString* embedHTML = @"<iframe type=\"text/html\" width=\"64\" height=\"39\" src=\"http://www.youtube.com/embed/j8Bc7eRTdWY\" frameborder=\"0\"></iframe>";
    if(videoView == nil) {          
        videoView = [[UIWebView alloc] initWithFrame:frame];  
        [movieDescView addSubview:videoView];  
    }  
    [videoView loadHTMLString:embedHTML baseURL:nil];  
}

The problem is when I clicked on the play icon, it opens the youtube website with my uiwebview asking to install some web app . I would like to prevent this web app pop up to show or start the video full screen. Actually, any solution to show a youtube video without exiting my app would do.

Best Answer

You can use this to load the movie (no needed to generate a html string):

movieView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 140, 100)];
movieView.delegate = self;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.youtube.com/embed/-h4zTEwgCpQ"]];
[movieView loadRequest:request];
[self.view addSubview:movieView];


#pragma mark -
#pragma mark UIWebViewDelegate
//To check if youtube try to show the overview page of m.youtube.com or the current movie
//if the user click on the youtube logo this method stop loading the mobile page
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSURL *urlRequest = [request URL];

    if ([[urlRequest absoluteString] isEqualToString:url]) {
        return YES;
    }

    return NO;
}

The onely thing you have to looking for, is to use the "embed" link.

Related Topic