h5与安卓、ios交互

1.安卓交互

     h5调用安卓方法

       window.webview.xxx()

     安卓调用h5方法, 方法需要在全局注册

    

window['showUnreadMsg'] = () => {
	this.$nextTick(() => {
		this.showUnreadMsg();
	})
}

  

 2. ios交互

    

function setupWebViewJavascriptBridge(callback) {
  if (window.WebViewJavascriptBridge) {
    return callback(window.WebViewJavascriptBridge)
  }
  if (window.WVJBCallbacks) {
    return window.WVJBCallbacks.push(callback)
  }
  window.WVJBCallbacks = [callback]
  let WVJBIframe = document.createElement('iframe')
  WVJBIframe.style.display = 'none'
  WVJBIframe.src = 'https://__bridge_loaded__'
  document.documentElement.appendChild(WVJBIframe)
  setTimeout(() => {
    document.documentElement.removeChild(WVJBIframe)
  }, 0)
}

  h5调用ios方法

   

setupWebViewJavascriptBridge(function(bridge){
    bridge.callHandler('getPhoneNumber',(data) => {
        that.loginApp(data)	
    })
})        

ios调用h5方法, 同样需要全局注册

setupWebViewJavascriptBridge(function(bridge){
  bridge.registerHandler('showUnreadMsg',(data,responseCallback)=>{
	if(responseCallback){
		that.showUnreadMsg();
	}
  })
})

  

猜你喜欢

转载自www.cnblogs.com/THONLY/p/11303011.html