Victor Van Doom
Victor Van Doom

Reputation: 103

Using share functionality with embedded youtube iframe in iOS

as the title already states I am facing a few problems with the embedded youtube iframe-player on iOS. But first a little bit about the background of my problem. I have a set of HTML-files that are stored on the device and loaded inside the a WKWebView. The files contain only simple text and sometimes a youtube-video embedded using the iframe-syntax.

<iframe width="100%" height="100%" id="playerId" type="text/html" src="https://www.youtube.com/embed/XXXXXXXX?enablejsapi=0&rel=0&playsinline=0&autoplay=0" frameborder="0">

Playback, sound, entering fullscreen.... everything works as expected. Everything except for the info section of the youtube-player (the arrow in the upper right corner). When I press the arrow the screen opens, but selecting the url or one of the share options has no effect but to break the embedded video. I can still close the info section, but the thumbnail disappears and it won't play until I reload the HTML-document.

Now my guess would be that there are security settings in the WKWebView sandbox that prevent me from loading any embedded link. I think I remember something about the WebView not allowing links with a _blank target in iframes, but I am not sure about that. Couldn't change those anyhow.

Thanks for any hint,

Victor

Upvotes: 2

Views: 408

Answers (1)

Victor Van Doom
Victor Van Doom

Reputation: 103

OK,

I've managed to solve the problem myself (at least partially). I took a look into the header of WKWebView and its Delegates and found a comment that mentioned the WebViews behavior when opening links with a _blank target. As the WebView cannot open a new window (that's what the blank enforces) it will call its UIDelegate to request a new WKWebView instance to open the "new window" link (see also https://developer.apple.com/reference/webkit/wkuidelegate/1536907-webview).

What I did now was to implement the delegate, check if the navigationActions targetFrame is nil (which means that it points to a new Window) and open the link in Safari instead. Unfortunately, this kind of link handling always breaks the player, so I also have to trigger a reload of my embedded web-content in order to "reset" the player back to its initial state. This is a bit unfortunate with regard to resuming the playback when returning to the App, but its better than not being able to play the video at all.

func webView(webView: WKWebView, createWebViewWithConfiguration configuration: WKWebViewConfiguration, forNavigationAction navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {

    if navigationAction.targetFrame == nil { 

        guard let requestURL = navigationAction.request.URL
            else { return nil }

        UIApplication.sharedApplication().openURL(requestURL)
        webView.reload()
    }

    return nil
}

Upvotes: 2

Related Questions