网页通过webview加载,webview作为native和js交互的中介
ios
ios调用js
webview.stringByEvaluatingJavaScriptFromString:返回js脚本的执行结果
// Swift
webview.stringByEvaluatingJavaScriptFromString("swiftcalljs()")
js调用ios
jsbridge://开头的请求在native层进行调用逻辑
通过新建iframe(location.href只会处理多个请求的最后一个)
var url = 'jsbridge://doAction?title=分享标题&desc=分享描述&link=http%3A%2F%2Fwww.baidu.com';
var iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.src = url;
document.body.appendChild(iframe);
setTimeout(function() {
iframe.remove();
}, 100);
Android
Native调用js
webView.loadUrl("javascript:javacalljs()");
js调用native
JavaScriptInterface类定义了js可以调用的方法
//js端
JSInterface.changeActivity();