Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link "Watch on youtube" doesn't work from iOS UIWebView?

I've embedded a youtube iFrame to play videos in a UIWebView web page.

The link "watch on youtube" doesn't work:

enter image description here

The code is:

<iframe name="player" id="player" src="http://www.youtube.com/embed/{$firstParsedVideo}?HD=1;rel=0;showinfo=0" width="279" height="155"></iframe>

where $firstParsedVideo is the video id.

I've done the same in a Cocoa Osx WebView and it works perfectly.

Thanks

like image 725
aneuryzm Avatar asked Nov 26 '22 09:11

aneuryzm


1 Answers

here is the real solution to the problem:

the "watch on youtube" link has a target = open in new window property, to circumvent this, here is the relevant code:

- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {
  if (!navigationAction.targetFrame.isMainFrame) {
    [webView loadRequest:navigationAction.request];
  }
  return nil;
}

in swift:

extension YoutubeView: WKUIDelegate {
    func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
        if !(navigationAction.targetFrame?.isMainFrame ?? false) {
            webView.load(navigationAction.request)
        }
        return nil
    }
}
like image 190
Halpo Avatar answered Nov 28 '22 23:11

Halpo