当前位置:网站首页>The interactive solution of JS and app in the H5 page embedded in app (parameters can be transferred and callbacks can be made)
The interactive solution of JS and app in the H5 page embedded in app (parameters can be transferred and callbacks can be made)
2022-07-04 04:22:00 【Careteen】
Preface
During the rapid iteration of the project ,APP Embedded in H5 Page is already a very common practice .
There will be APP and JS Interactive scenarios , for example JS evoke APP And carry parameters ...
Interactive mode
Method 1 :app End interception and h5 End agreed specific url
// Without reference window.location.href = 'https://xxx.focus.cn/backtoapp' // Banded ginseng window.location.href = 'https://xxx.focus.cn/backtoapp?data=xxx'
The problem is :
- Some of the solutions are to avoid url Defects in length , stay iOS The use of Ajax How to send the same domain request , And put the parameters in head or body in . such , Although I evaded url The hidden danger of length , however WKWebView I don't support this way .
- Why choose iframe.src No choice locaiton.href ? Because if you pass location.href Continuous call Native, It's easy to lose some calls .
- Modify continuously for many times window.location.href Value , stay Native The layer can only receive the last request , All previous requests will be ignored .
- Only solved js Call the native problem . As for the result of the call and the callback of some pages after the call , Through this interception url There is no way to do it .
Method 2 : Use WebViewJavascriptBridge
Essentially , It's through webview
Agent intercepts scheme
, Then inject the corresponding JS
.
Method 3 : Use webkit MessageHandler
The principle is the same as WebViewJavascriptBridge
This library
This library mainly uses WebViewJavascriptBridge
and webkit MessageHandler
encapsulate .
Use WebViewJavascriptBridge
and webkit MessageHandler
APP End
H5 End
principle : H5 page <--> Native App
Execution is called Native
The code returns the call result (H5 Page execution is called JavaScript Code and return the call result )
encapsulation bridge.js.
index.html Use in :
<button id="btn"> Simulate call login with parameters and callback </button>
index.js Use in :
require('/path/to/bridge.js'); // You need to agree the method name, parameters and callback of mutual calls with the client students in advance , Functions needed for the package HFWVBridge.wrapNativeFn(['login']); document.getElementById('btn').onclick = function() { // Android End if used messageHandlers The way ,HFWVBridge that will do ; // If not, use WebViewJavascriptBridge , Then use window.WebViewJavascriptBridge.callHandler if (isFocusAppIOS()) { HFWVBridge.runNative('login', { name: ' Sohu netizens ' }, function() { alert(' Welcome to Sohu '); }) } else { /** * * @desc Mode one : Send a message to app * @param {Any} Message sent * @param {Function} Send a message to app Callback after , And can get app Returned data */ var data = {id: 1, content: " This is a picture <img src=\"a.png\"/> test\r\nhahaha"}; window.WebViewJavascriptBridge.send( data , function(responseData) { document.getElementById("show").innerHTML = "repsonseData from java, data = " + responseData } ); /** * * @desc Mode two : call app Methods * @param {String} The call method name agreed with the client in advance * @param {Object} call app The transmission of the method * @param {Function} call app Method callback , And can get app Returned data */ window.WebViewJavascriptBridge.callHandler( 'submitFromWeb' , {'param': ' Chinese test '} , function(responseData) { document.getElementById("show").innerHTML = "send get responseData from java, data = " + responseData } ); } }; HFWVBridge.add('hideBtn',function(){ document.getElementById('btn').style.display = 'none'; });
Advantages and disadvantages
advantage
- The mobile terminal no longer needs to intercept jump links , Hard coding is reduced .
- Support bidirectional callback , Support asynchronous callback .
- High safety .
shortcoming
- JS、IOS、Android There are many initializations of three terminal code , It's a little bit more complicated . Need a big boss , Problems can be repaired in time .
quote
边栏推荐
猜你喜欢
Flink学习7:应用程序结构
*. No main manifest attribute in jar
北漂程序员,月薪20K,一年攒15W,正常吗?
My opinion on how to effectively telecommute | community essay solicitation
Unity 绘制弹球和台球的运动轨迹
Tcp- simple understanding of three handshakes and four waves
Pointer array and array pointer
Two commonly used graphics can easily realize data display
透过JVM-SANDBOX源码,了解字节码增强技术原理
leetcode刷题:二叉树06(对称二叉树)
随机推荐
RHCSA 07 - 用户与群组管理
1289_ Implementation analysis of vtask suspend() interface in FreeRTOS
SDP中的SPA
一位毕业生的自我分享
C语言双向链表初版
Leetcode skimming: binary tree 07 (maximum depth of binary tree)
I was tortured by my colleague's null pointer for a long time, and finally learned how to deal with null pointer
The maximum expiration time of client secret in azure ad application registration is modified to 2 years
User defined path and file name of Baidu editor in laravel admin
Interpretation of leveldb source code skiplist
Activiti7 task service - process variables (setvariable and setvariablelocal)
Brief explanation of depth first search (with basic questions)
分布式系统:what、why、how
Katalon使用script实现查询List大小
Leetcode brush question: binary tree 06 (symmetric binary tree)
How to add custom API objects in kubernetes (1)
Lnk2038 detected a mismatch of "runtimelibrary": the value "md_dynamicrelease" does not match the value "mdd_dynamicdebug" (in main.obj)
[webrtc] M98 Ninja build and compile instructions
(pointeur) Écrivez - vous une fonction qui compare la taille de la chaîne et fonctionne comme strcmp.
【webrtc】m98 ninja 构建和编译指令