当前位置:   article > 正文

H5与原生交互方式

H5与原生交互方式

使用的组件dsbridge.js

  1. var bridge = {
  2. default:this,// for typescript
  3. call: function (method, args, cb) {
  4. var ret = '';
  5. if (typeof args == 'function') {
  6. cb = args;
  7. args = {};
  8. }
  9. var arg={data:args===undefined?null:args}
  10. if (typeof cb == 'function') {
  11. var cbName = 'dscb' + window.dscb++;
  12. window[cbName] = cb;
  13. arg['_dscbstub'] = cbName;
  14. }
  15. arg = JSON.stringify(arg)
  16. //if in webview that dsBridge provided, call!
  17. if(window._dsbridge){
  18. ret= _dsbridge.call(method, arg)
  19. }else if(window._dswk||navigator.userAgent.indexOf("_dsbridge")!=-1){
  20. ret = prompt("_dsbridge=" + method, arg);
  21. }
  22. return JSON.parse(ret||'{}').data
  23. },
  24. register: function (name, fun, asyn) {
  25. var q = asyn ? window._dsaf : window._dsf
  26. if (!window._dsInit) {
  27. window._dsInit = true;
  28. //notify native that js apis register successfully on next event loop
  29. setTimeout(function () {
  30. bridge.call("_dsb.dsinit");
  31. }, 0)
  32. }
  33. if (typeof fun == "object") {
  34. q._obs[name] = fun;
  35. } else {
  36. q[name] = fun
  37. }
  38. },
  39. registerAsyn: function (name, fun) {
  40. this.register(name, fun, true);
  41. },
  42. hasNativeMethod: function (name, type) {
  43. return this.call("_dsb.hasNativeMethod", {name: name, type:type||"all"});
  44. },
  45. disableJavascriptDialogBlock: function (disable) {
  46. this.call("_dsb.disableJavascriptDialogBlock", {
  47. disable: disable !== false
  48. })
  49. }
  50. };
  51. !function () {
  52. if (window._dsf) return;
  53. var _close=window.close;
  54. var ob = {
  55. //保存JS同步方法
  56. _dsf: {
  57. _obs: {}
  58. },
  59. //保存JS异步方法
  60. _dsaf: {
  61. _obs: {}
  62. },
  63. dscb: 0,
  64. dsBridge: bridge,
  65. close: function () {
  66. if(bridge.hasNativeMethod('_dsb.closePage')){
  67. bridge.call("_dsb.closePage")
  68. }else{
  69. _close.call(window)
  70. }
  71. },
  72. _handleMessageFromNative: function (info) {
  73. var arg = JSON.parse(info.data);
  74. var ret = {
  75. id: info.callbackId,
  76. complete: true
  77. }
  78. var f = this._dsf[info.method];
  79. var af = this._dsaf[info.method]
  80. var callSyn = function (f, ob) {
  81. ret.data = f.apply(ob, arg)
  82. bridge.call("_dsb.returnValue", ret)
  83. }
  84. var callAsyn = function (f, ob) {
  85. arg.push(function (data, complete) {
  86. ret.data = data;
  87. ret.complete = complete!==false;
  88. bridge.call("_dsb.returnValue", ret)
  89. })
  90. f.apply(ob, arg)
  91. }
  92. if (f) {
  93. callSyn(f, this._dsf);
  94. } else if (af) {
  95. callAsyn(af, this._dsaf);
  96. } else {
  97. //with namespace
  98. var name = info.method.split('.');
  99. if (name.length<2) return;
  100. var method=name.pop();
  101. var namespace=name.join('.')
  102. var obs = this._dsf._obs;
  103. var ob = obs[namespace] || {};
  104. var m = ob[method];
  105. if (m && typeof m == "function") {
  106. callSyn(m, ob);
  107. return;
  108. }
  109. obs = this._dsaf._obs;
  110. ob = obs[namespace] || {};
  111. m = ob[method];
  112. if (m && typeof m == "function") {
  113. callAsyn(m, ob);
  114. return;
  115. }
  116. }
  117. }
  118. }
  119. for (var attr in ob) {
  120. window[attr] = ob[attr]
  121. }
  122. bridge.register("_hasJavascriptMethod", function (method, tag) {
  123. var name = method.split('.')
  124. if(name.length<2) {
  125. return !!(_dsf[name]||_dsaf[name])
  126. }else{
  127. // with namespace
  128. var method=name.pop()
  129. var namespace=name.join('.')
  130. var ob=_dsf._obs[namespace]||_dsaf._obs[namespace]
  131. return ob&&!!ob[method]
  132. }
  133. })
  134. }();
  135. //
  136. // module.exports = bridge;

H5主动调用原生方法以及返回数据

  1. dsBridge.call('device.getDeviceNum',function(v){
  2. var json = JSON.parse(v)
  3. console.log('获取到的原生设备',json)
  4. })
  1. function echoSyn() {
  2. // call function with namespace
  3. var ret=dsBridge.call("echo.syn",{msg:" I am echoSyn call", tag:1});
  4. alert(JSON.stringify(ret))
  5. }


原生主动调用H5方法

原生代码

  1. [dwebview callHandler:@"addValue" arguments:@[@3,@4] completionHandler:^(NSNumber * value){
  2. NSLog(@"%@",value);
  3. }];
  4. [dwebview callHandler:@"syn.addValue" arguments:@[@5,@6] completionHandler:^(NSDictionary * _Nullable value) {
  5. NSLog(@"Namespace syn.addValue(5,6): %@",value);
  6. }];

H5端代码
 

  1. dsBridge.register('addValue', function (r, l) {
  2. return r + l;
  3. })
  4. // namespace test for syn functions
  5. dsBridge.register("syn", {
  6. tag: "syn",
  7. addValue:function (r,l) {
  8. return r+l;
  9. }
  10. })

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

闽ICP备14008679号