码迷,mamicode.com
首页 > 移动开发 > 详细

webapp js与安卓,ios怎么交互

时间:2018-10-19 00:16:48      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:scheme   mac os x   webapp   ffffff   idg   怎么   tor   sch   func   

/*这段代码是固定的,必须要放到js中*/
function setupWebViewJavascriptBridge(callback) {

var u = navigator.userAgent;
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
if (!isiOS) {
if (window.WebViewJavascriptBridge) {
callback(WebViewJavascriptBridge)
} else {
document.addEventListener(‘WebViewJavascriptBridgeReady‘, function (event) {
if(window.onWebViewJavascriptBridgeReady) window.onWebViewJavascriptBridgeReady(window.__bridge = WebViewJavascriptBridge);
callback(WebViewJavascriptBridge)
}, false)
}
} else {

if (window.WebViewJavascriptBridge) {
return callback(WebViewJavascriptBridge);
}
if (window.WVJBCallbacks) {
return window.WVJBCallbacks.push(callback);
}
window.WVJBCallbacks = [callback];
var WVJBIframe = document.createElement(‘iframe‘);
WVJBIframe.style.display = ‘none‘;
WVJBIframe.src = ‘wvjbscheme://__BRIDGE_LOADED__‘;
document.documentElement.appendChild(WVJBIframe);
setTimeout(function() {
document.documentElement.removeChild(WVJBIframe)
}, 0)
}

}

export default {

callhandler (name, data, callback) {
setupWebViewJavascriptBridge(function (bridge) {
bridge.callHandler(name, data, callback)
})
},

registerhandler (name, callback) {
setupWebViewJavascriptBridge(function (bridge) {
bridge.registerHandler(name, function (data, responseCallback) {
callback(data, responseCallback)
})
})
}
}
// 调用
this.$bridge.callhandler(‘shopCode‘,{‘test‘:‘test‘},(data) => {
this.shopCode = data.shopCode;
})

webapp js与安卓,ios怎么交互

标签:scheme   mac os x   webapp   ffffff   idg   怎么   tor   sch   func   

原文地址:https://www.cnblogs.com/langqq/p/9813296.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!