• js与ios桥接使用WebViewJavascriptBridge简单理解


    https://github.com/marcuswestin/WebViewJavascriptBridge

    function setupWebViewJavascriptBridge(callback) {
        if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
        if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
        window.WVJBCallbacks = [callback];
        var WVJBIframe = document.createElement('iframe');
        WVJBIframe.style.display = 'none';
        WVJBIframe.src = 'https://__bridge_loaded__';
        document.documentElement.appendChild(WVJBIframe);
        setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
    }
    Finally, call setupWebViewJavascriptBridge and then use the bridge to register handlers and call ObjC handlers:
    setupWebViewJavascriptBridge(function(bridge) {
        
        /* Initialize your app here */
    
        bridge.registerHandler('JS Echo', function(data, responseCallback) {
            console.log("JS Echo called with:", data)
            responseCallback(data)
        })
        bridge.callHandler('ObjC Echo', {'key':'value'}, function responseCallback(responseData) {
            console.log("JS received response:", responseData)
        })
    })

    github地址如上

    工作中用到了这个桥接,出现了很多问题,

    首先,初始化了这个函数之后,然后调用这个函数,调用ios端定义的handler或者js 注册自己的handler,

    但是好像不可以写多个该函数的调用,否则所有写的交互不生效。

    那么我如果想调用ios定义的多个函数,只能写在一个调用里,罗列多个

     bridge.registerHandler 

    bridge.callHandler来写了。
    目前还是出现了个别交互不生效的问题。

    registerHandler 就是在网页端定义一个函数,获取后端返回的相应参数,后端调用使用
    callHandler  就是在ios端定义一个函数,网页端调用,传过去相应参数。
     

    我的博客即将搬运同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=dd4qxupt8clk
     
  • 相关阅读:
    spark 随意笔记
    c#读取输入字符串,从数据源中查找以该字符串开头的所有字符串(使用正则表达式)
    我的收藏链接地址
    SQL查询时,遇到用到关键词作的字段。将该字段用一对中括号括起来[]
    SQL数据类型相互转换
    Javascript获取系统当前时间
    节点类型nodeType的取值
    混合布局编程挑战
    Webstorm破解方法
    二列布局
  • 原文地址:https://www.cnblogs.com/beileixinqing/p/8780904.html
Copyright © 2020-2023  润新知