// js 原生交互代码必写
function connectWebViewJavascriptBridge(callback) {
if (window.WebViewJavascriptBridge) {
callback(window.WebViewJavascriptBridge);
} else {
document.addEventListener(
'WebViewJavascriptBridgeReady',
function() {
callback(window.WebViewJavascriptBridge);
},
false,
);
if (!isAndroid) {
if (window.WebViewJavascriptBridge) {
return callback(window.WebViewJavascriptBridge);
}
if (window.WVJBCallbacks) {
return window.WVJBCallbacks.push(callback);
}
window.WVJBCallbacks = [callback];
const WVJBIframe = document.createElement('iframe');
WVJBIframe.style.display = 'none';
WVJBIframe.src = 'https://__bridge_loaded__';
document.documentElement.appendChild(WVJBIframe);
setTimeout(function() {
document.documentElement.removeChild(WVJBIframe);
}, 0);
}
}
}
// 初始化 ios
connectWebViewJavascriptBridge(function(bridge) {
bridge.registerHandler('callRefreshAccessToken', function(data, cb) {
//回调函数里处理自己的业务逻辑
console.log(data, callbackAll);
});
});
// ios 处理err
connectWebViewJavascriptBridge(function(bridge) {
bridge.registerHandler('accessTokenError', function(data, cb) {
console.log('报错了')
});
});
// 初始化 android
window.callRefreshAccessToken = function(data) {
// 获取原生传递的token 信息
console.log(data, callbackAll);
};
// android err
window.accessTokenError = function() {
console.log('报错了')
};
- 其中,
callRefreshAccessToken
和 accessTokenError
函数名是和原生协商定的(根据自己项目的协议改)
网友评论