I have a WKWebview
in an UIViewController
where do i need to display a youtube channel video.
I'm using an html that has an iframe
that points to the youtube embed url, where I pass the id of the video I want to display.
With this in mind, I created the function, which build the html string to load in webview.
static func buildYoutubeEmbedHTML(with id: String) -> String {
let frameHTML = """
<html>
<head>
<style>
body, html, iframe { margin: 0; padding: 0;}
</style>
</head>
<body>
<iframe width="100%" height="100%" src="https://www.youtube.com/embed/\(id)" frameborder="0" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</body>
</html>
"""
return frameHTML
}
The problem is that the following output started to be displayed in the console:
[Security] This method should not be called on the main thread as it may lead to UI unresponsiveness.
I noticed that the error is generated when trying to load the generated html using:
webview.loadHTMLString(html, baseURL: Bundle.main.resourceURL)
However, if I change the HTML and remove the iframe
and put a simple h1
tag for example, it doesn't generate the problem. Has anyone experienced this, or have any idea why this might occur, and is there any workaround to get around the problem? Or even some other element that can replace the iframe
?
Thanks in advance