html – Swift WKUserContentController and WKScriptMessageHandler will not be arrange proper

Spread the love

Hiya I’m utilizing WKUserContentController and WKScriptMessageHandler to ship a message from my embed html to swift to be able to change a price of a bool. Once I run the code beneath the onReady block is executed however the message is rarely despatched. The if assertion to ship the message is evaluated to false which implies that the webView is nil and never arrange proper. Once I run the app on my iPhone, join my cellphone to my Mac, navigate to the web site on my Mac, press “Develop”, then hover over my cellphone, I do not see any net views open. Because of this the net view isn’t arrange proper. This query is an extension of Find out how to talk from embedded HTML to Swift to vary bool. The aim of sending the message is to speak from the html to swift to let the view know when the video is able to play. Id recognize any assist to determine learn how to get the message despatched.

struct SmartReelView: UIViewRepresentable {
    let hyperlink: String
    @Binding var isPlaying: Bool
    func makeCoordinator() -> Coordinator {
    func makeUIView(context: Context) -> WKWebView {
        let webConfiguration = WKWebViewConfiguration()
        webConfiguration.allowsInlineMediaPlayback = true
        let webView = WKWebView(body: .zero, configuration: webConfiguration)
        webView.navigationDelegate = context.coordinator

        let userContentController = WKUserContentController()

        userContentController.add(context.coordinator, title: "toggleMessageHandler")
        webView.configuration.userContentController = userContentController

        loadInitialContent(in: webView)
        return webView

    func updateUIView(_ uiView: WKWebView, context: Context) {
        let jsString = "isPlaying = ((isPlaying) ? "true" : "false"); watchPlayingState();"
        uiView.evaluateJavaScript(jsString, completionHandler: nil)
    class Coordinator: NSObject, WKScriptMessageHandler, WKNavigationDelegate {
        var father or mother: SmartReelView

        init(_ father or mother: SmartReelView) {
            self.father or mother = father or mother

        func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
            print("message recieved")
            self.father or mother.isPlaying = true
    non-public func loadInitialContent(in webView: WKWebView) {
        let embedHTML = """
            .iframe-container iframe {
                high: 0;
                left: 0;
                width: 100%;
                top: 100%;
        <div class="iframe-container">
            <div id="participant"></div>
            var tag = doc.createElement('script');
            tag.src = "";
            var firstScriptTag = doc.getElementsByTagName('script')[0];
            firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

            var participant;
            var isPlaying = true;
            perform onYouTubeIframeAPIReady() {
                participant = new YT.Participant('participant', {
                    width: '100%',
                    videoId: '(hyperlink)',
                    playerVars: { 'playsinline': 1, 'controls': 0},
                    occasions: {
                        'onReady': perform(occasion) {
                            if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.toggleMessageHandler) {
                                window.webkit.messageHandlers.toggleMessageHandler.postMessage({"message": "click on now" });
            perform watchPlayingState() {
                if (isPlaying) {
                } else {
        webView.scrollView.isScrollEnabled = false
        webView.loadHTMLString(embedHTML, baseURL: nil)

Leave a Reply

Your email address will not be published. Required fields are marked *