johnslay
johnslay

Reputation: 691

WKWebView not loading content from iframe on a webpage

I have a WKWebView where a user goes through several pages to reach a page which contains a video from lesson.ly.

The problem is that the video will not show up at all within the webview, there is just a blank gap in the page where the video would be. However, if I open chrome on iOS, it loads just fine.

I've pinpointed the issue by debugging the HTML code. Here is a screen shot of the HTML on the webview: enter image description here

And Here's a picture of the HTML on Chrome for Mac: enter image description here

If you noticed, there is nothing within the iframe for the webview. I have no idea why it's not loading the data. I have enabled 'Allows Arbitrary Loads' for App Transport Security to no avail.

Any help appreciated, thanks.

Upvotes: 4

Views: 16623

Answers (2)

Alex Staravoitau
Alex Staravoitau

Reputation: 2268

Alternatively, instead of allowing specific URLs, you can check if request in question is targeting main frame or not:

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void) {
    // Allow navigation for requests loading external web content resources.
    guard navigationAction.targetFrame?.isMainFrame != false else {
        decisionHandler(.allow)
        return
    }

    ...
}

Upvotes: 10

johnslay
johnslay

Reputation: 691

Figured out the problem. It was all in the delegate method webView:decidePolicyForNavigationAction:decisionHandler: and because the Lessonly videos come from a different source (fast.wistia.net) I had to add that url expliclity.

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {

    guard let requestURL = navigationAction.request.url?.absoluteString else { return }

    if requestURL.tc_contains("mydomain.lesson.ly") || requestURL.tc_contains("fast.wistia.net"){
        decisionHandler(.allow)
    }
    else {
        decisionHandler(.cancel)
    }
}

Upvotes: 6

Related Questions