Problem embedding an youtube video on iphone

2019-07-19 17:23发布

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.

5条回答
萌系小妹纸
2楼-- · 2019-07-19 18:08

I spent quite a bit of time looking through libraries and old code snippets; this library worked for me:

https://github.com/larcus94/LBYouTubeView

查看更多
贪生不怕死
3楼-- · 2019-07-19 18:11

You don't use embed if you are trying to play the video. Just use the normal video URL so it will open in the iPhone's YouTube application rather than the web. If it opens in the web it will ask to install it's web app.

You need to change your HTML to this:

<a href="http://www.youtube.com/watch?v=j8Bc7eRTdWY">Play Video</a>

Or to launch it directly to the native app use the URL:

http://www.youtube.com/watch?v=j8Bc7eRTdWY

More Reference Information:

Apple Reference on URL Schemes

Apple Reference on Youtube Links

查看更多
神经病院院长
4楼-- · 2019-07-19 18:13
UIWebView  *wbView = (UIWebView *)[self.view viewWithTag:1];
NSString *embedHTML = @"\
    <html><head>\
    <style type=\"text/css\">\
    body {\
    background-color: transparent;\
    color: white;\
    }\
    </style>\
    </head><body style=\"margin:0\">\
    <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
    width=\"%0.0f\" height=\"%0.0f\"></embed>\
    </body></html>";

    NSString *html = [NSString stringWithFormat:embedHTML,url, 64.0, 64.0];
    [wbView loadHTMLString:html baseURL:nil];

  "url" is your video url
查看更多
时光不老,我们不散
5楼-- · 2019-07-19 18:16

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.

查看更多
兄弟一词,经得起流年.
6楼-- · 2019-07-19 18:18

Tips:

stringUrl = [stringUrl stringByReplacingOccurrencesOfString:@"/watch?v=" withString:@"/embed/"];

where stringUrl is a video link of youtube ( ex. Youtube )

查看更多
登录 后发表回答