h5和android交互方法:https://www.jianshu.com/p/53a44780a34f
首先 不管是ios调用h5,还是h5调用ios的方法都需要定义JavascriptBridge
/** 建立桥 */
function setupWebViewJavascriptBridge ( callback) {
if (window['WebViewJavascriptBridge']) {
return _callback(window['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(() => {
document.documentElement.removeChild(WVJBIframe);
}, 0);
}
一、ios调用h5方法: 使用registerHandler
setupWebViewJavascriptBridge((bridge) => {
bridge.registerHandler(name:“ios调用h5的方法名”, function(data, responseCallback) {
console.log('do something')
responseCallback(data)
})
});
二、h5调用IOS的方法:callHandler
setupWebViewJavascriptBridge((_bridge) => {
_bridge.callHandler(name:H5调用IOS的方法名, data:传给ios的参数, (_res) => {
/** 回调,可以不用*/
callback();
})
});