AgentWeb调用JsBridge采坑记录
AgentWeb:https://github.com/Justson/AgentWeb
JsBridge:https://github.com/lzyzsd/JsBridge
JS需添加
function connectWebViewJavascriptBridge(callback) {
if (window.WebViewJavascriptBridge) {
callback(WebViewJavascriptBridge)
} else {
document.addEventListener(
'WebViewJavascriptBridgeReady'
, function() {
callback(WebViewJavascriptBridge)
},
false
);
}
}
connectWebViewJavascriptBridge(function(bridge) {
bridge.init(function(message, responseCallback) {
console.log('Js got a message', message);
var data = {
'Javascript Responds': '测试中文!'
};
console.log('Js responding with', data);
responseCallback(data);
});
})
WebViewClient
private WebViewClient getWebViewClient() {
return new WebViewClient() {
BridgeWebViewClient mBridgeWebViewClient = new BridgeWebViewClient(mBridgeWebView);
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
NLog.i(Constants.TAG, "url: " + request.getUrl().toString());
return mBridgeWebViewClient.shouldOverrideUrlLoading(view, request); //兼容高版本,必须设置
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
NLog.i(Constants.TAG, "url: " + url);
return mBridgeWebViewClient.shouldOverrideUrlLoading(view, url); //兼容低版本,必须设置
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
mBridgeWebViewClient.onPageStarted(view, url, favicon); //必须设置
}
@Override
public void onPageFinished(WebView view, String url) { //必须设置
mBridgeWebViewClient.onPageFinished(view, url);
}
@TargetApi(Build.VERSION_CODES.M)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
//yy://__QUEUE_MESSAGE__/
LogUtils.i(TAG, "onReceivedError:" + error.getDescription() + " code:" + error.getErrorCode() + ", url: " + request.getUrl().toString());
}
};
}
必须设置WebViewClient,并且其生命周期交由BridgeWebViewClient处理,否则报错:
agentweb - DefaultWebClient: onReceivedError:net::ERR_UNKNOWN_URL_SCHEME code:-10
版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/18/agentweb-calls-jsbridge/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。
THE END
0
二维码
打赏
海报
AgentWeb调用JsBridge采坑记录
AgentWeb:https://github.com/Justson/AgentWeb
JsBridge:https://github.com/lzyzsd/JsBridge
JS需添加
function connectWebViewJavascriptBridge(callbac……
文章目录
关闭
共有 0 条评论