当前位置:   article > 正文

使用jsBridge实现H5与原生App交互_jsbridge的h5

jsbridge的h5
  1. var u = navigator.userAgent;
  2. window.isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
  3. window.isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
  4. window.jsbridge = function (callback) {
  5. if(window.isAndroid){
  6. if (window.WebViewJavascriptBridge) {
  7. return callback(WebViewJavascriptBridge);
  8. } else {
  9. document.addEventListener(
  10. 'WebViewJavascriptBridgeReady',
  11. function () {
  12. callback(WebViewJavascriptBridge)
  13. },
  14. false
  15. );
  16. }
  17. }
  18. if (window.isIOS) {
  19. // new ios method
  20. // if (window.WKWebViewJavascriptBridge) { return callback(WKWebViewJavascriptBridge); }
  21. // if (window.WKWVJBCallbacks) { return window.WKWVJBCallbacks.push(callback); }
  22. // window.WKWVJBCallbacks = [callback];
  23. // window.webkit.messageHandlers.iOS_Native_InjectJavascript.postMessage(null)
  24. setupWebViewJavascriptBridge(callback);
  25. }
  26. }
  27. //初始化 这段代码的意思就是执行加载WebViewJavascriptBridge_JS.js中代码的作用
  28. function setupWebViewJavascriptBridge(callback) {
  29. console.log("setupWebViewJavascriptBridge");
  30. if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
  31. console.log("WebViewJavascriptBridge complte");
  32. if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
  33. console.log("WVJBCallbacks complte");
  34. window.WVJBCallbacks = [callback];
  35. var WVJBIframe = document.createElement('iframe');
  36. WVJBIframe.style.display = 'none';
  37. WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
  38. document.documentElement.appendChild(WVJBIframe);
  39. setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
  40. }
  41. if (!window.isIOS) {
  42. jsbridge(function (bridge) {
  43. bridge.init(function (message, responseCallback) {
  44. var data = {};
  45. responseCallback(data);
  46. });
  47. })
  48. }
  49. /**
  50. * @param {option对象}
  51. * method: 调用的方法
  52. * data: 传递的参数
  53. * callback: 回掉方法
  54. * @return {[type]}
  55. */
  56. function bridgeCallHandler(option) {
  57. // alert(option.method)
  58. jsbridge(function (bridge) {
  59. bridge.callHandler(
  60. option.method,
  61. option.data || {},
  62. function (responseData) {
  63. if (option.callback) {
  64. option.callback(responseData)
  65. }
  66. }
  67. );
  68. })
  69. }
  70. //原生调js
  71. let registerHandler = function (method, callback) {
  72. // alert(option.method);
  73. jsbridge(function (bridge) {
  74. bridge.registerHandler(
  75. method,
  76. function (data, responseCallback) {
  77. if (callback) {
  78. callback(data);
  79. }
  80. responseCallback(data || {});
  81. }
  82. );
  83. })
  84. }
  85. /**
  86. * 传入的option对象格式
  87. * {
  88. * method:[String],
  89. * data:[Object],
  90. * callback:[Function]
  91. * }
  92. */
  93. // js调用原生
  94. let callHandler = function (method, data, callback) {
  95. bridgeCallHandler({
  96. method,
  97. data,
  98. callback: function (responseData) {
  99. if (callback) {
  100. callback(responseData);
  101. }
  102. }
  103. })
  104. }
  105. export default{
  106. registerHandler,
  107. callHandler
  108. }
  109. //在main.js中引入
  110. import jsBridge from "@/utils/jsBridge"
  111. Vue.prototype.$jsBridge=jsBridge
  112. //在页面中使用
  113. mounted(){
  114. //监听app触发signatureSuccess,进行监听接收数据
  115. this.$jsBridge.registerHandler("signatureSuccess", (data)=>{
  116. console.log("监听获得的数据", data)
  117. })
  118. }
  119. methods:{
  120. signature(){
  121. // showCaBoard: js与原生约定的方法, params: js传递给原生的参数,
  122. this.$jsBridge.callHandler("showCaBoard", params, (data)=>{
  123. console.log(data)
  124. })
  125. }
  126. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/710358
推荐阅读
  

闽ICP备14008679号