// 页面与安卓或ios的交互
export const u = navigator.userAgent
export const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) // ios终端
export function interactiveBridge (sendName, receiveName, sendData, receiveCallback) {
if (isiOS) {
// ios终端
setupWebViewJavascriptBridge(function (bridge) {
// 注册任务
bridge.registerHandler(receiveName, function (data, responseCallback) {
receiveCallback(data)
})
// 发布任务
bridge.callHandler(sendName, sendData, function (response) {
})
})
} else {
// android终端
// 发布任务
(function () {
try {
// eslint-disable-next-line
javascript:android[sendName](JSON.stringify(sendData))
} catch (e) {
}
})()
// 这个方法用于接收code
window[receiveName] = function (data) {
receiveCallback(data)
}
}
}
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(function () {
document.documentElement.removeChild(WVJBIframe)
}, 0)
}
JS和安卓IOs的交互
猜你喜欢
转载自blog.csdn.net/AnlanJion/article/details/86489536
今日推荐
周排行