当前位置:网站首页>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
边栏推荐
- RHCSA 08 - automount配置
- Graduation project: design seckill e-commerce system
- Tcp- simple understanding of three handshakes and four waves
- Flink学习7:应用程序结构
- Leetcode skimming: binary tree 07 (maximum depth of binary tree)
- Redis:哈希hash类型数据操作命令
- STM32 external DHT11 display temperature and humidity
- hbuildx中夜神模拟器的配置以及热更新
- [Huawei cloud IOT] reading notes, "Internet of things: core technology and security of the Internet of things", Chapter 3 (I)
- Msgraphmailbag - search only driveitems of file types
猜你喜欢

软件测试是干什么的 发现缺陷错误,提高软件的质量

Flink learning 7: application structure

Keysight N9320B射频频谱分析仪解决轮胎压力监测方案

leetcode刷题:二叉树06(对称二叉树)
![[microservice openfeign] @feignclient detailed explanation](/img/8d/83bcde1355366c7c88a7a9ade7f9eb.png)
[microservice openfeign] @feignclient detailed explanation

Confession code collection, who says program apes don't understand romance

I was tortured by my colleague's null pointer for a long time, and finally learned how to deal with null pointer

The difference between bagging and boosting in machine learning

Msgraphmailbag - search only driveitems of file types

Idea configuration 360zip open by default -- external tools
随机推荐
透过JVM-SANDBOX源码,了解字节码增强技术原理
【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装
软件测试是干什么的 发现缺陷错误,提高软件的质量
统计遗传学:第三章,群体遗传
R语言中如何查看已安装的R包
Lnk2038 detected a mismatch of "runtimelibrary": the value "md_dynamicrelease" does not match the value "mdd_dynamicdebug" (in main.obj)
Myslq delete followed by limit
Programmers' telecommuting is mixed | community essay solicitation
Pointer array and array pointer
Brief explanation of depth first search (with basic questions)
思考的小记录
Tcp- simple understanding of three handshakes and four waves
R语言dplyr中的Select函数变量列名
"Implement both software and hardware" to help build a new cloud computing data center
vim正确加区间注释
[Yugong series] go teaching course 002 go language environment installation in July 2022
Redis:哈希hash类型数据操作命令
Perf simple process for multithreaded profile
Flink学习7:应用程序结构
干货!基于GAN的稀有样本生成