14.8 C
Friday, October 20, 2023

html – Swift WkMessageHandler message doesn’t ship

Hi there Im making a TikTok clone utilizing YouTube shorts and I want to get the video size. To take action I’ve created a perform in my embed html that posts a message utilizing WKScriptMessageHandler and the userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage){ perform is meant to choose up this message and replace the totalLength variable. However this at the moment doesn’t work. The message isn’t despatched and therefor the perform above by no means executes. However the html func sendCurrentTime() does execute when the WKWebView finishes loading. I will be posting a 400 bounty of this so Id recognize it rather a lot if somebody may also help work out why this message isn’t sending. Based mostly on my information the message doesn’t ship as a result of the net view is configured incorrectly or html doesn’t have entry to the net view object. Thanks.

struct SmartReelView: UIViewRepresentable {
    let hyperlink: String
    @Binding var totalLength: Double

    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: "observe")
        webview.configuration.userContentController = userContentController

        loadInitialContent(net: webview)
        return webview
    func updateUIView(_ uiView: WKWebView, context: Context) { }
    class Coordinator: NSObject, WKNavigationDelegate, WKScriptMessageHandler {
        var dad or mum: SmartReelView

        init(_ dad or mum: SmartReelView) {
            self.dad or mum = dad or mum
        func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage){
            if message.title == "observe", let Time = message.physique as? Double {
                dad or mum.totalLength = Time
        func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
            webView.evaluateJavaScript("sendCurrentTime()", completionHandler: nil)
    non-public func loadInitialContent(net: WKWebView) {
        let embedHTML = """
            physique {
                margin: 0;
                background-color: black;
            .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 = "https://www.youtube.com/iframe_api";
            var firstScriptTag = doc.getElementsByTagName('script')[0];
            firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

            var participant;
            var isPlaying = false;
            perform onYouTubeIframeAPIReady() {
                participant = new YT.Participant('participant', {
                    width: '100%',
                    videoId: '(hyperlink)',
                    playerVars: { 'playsinline': 1, 'controls': 0},
                    occasions: {
                        'onStateChange': perform(occasion) {
                            if (occasion.knowledge === YT.PlayerState.ENDED) {
            perform sendCurrentTime() {
                const size = participant.getDuration();
        net.scrollView.isScrollEnabled = false
        net.loadHTMLString(embedHTML, baseURL: nil)

Latest news
Related news


Please enter your comment!
Please enter your name here