h5 communicates with native app
bridge.js
01: Declare the bridge function between webview and ios and Android
connectWebViewJavascriptBridgeIOS builds a bridge function between h5 and ios
connectWebViewJavascriptBridgeANDROID builds a bridge function between h5 and Android
02: Declare that h5 calls the native function (callhandler)
function connectWebViewJavascriptBridgeIOS ( 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 = 'wvjbscheme://__BRIDGE_LOADED__'
document. documentElement. appendChild ( WVJBIframe)
setTimeout ( ( ) => {
document. documentElement. removeChild ( WVJBIframe)
} , 0 )
}
function connectWebViewJavascriptBridgeANDROID ( callback ) {
if ( window. WebViewJavascriptBridge) {
callback ( WebViewJavascriptBridge) ;
} else {
document. addEventListener (
"WebViewJavascriptBridgeReady" ,
function ( ) {
callback ( WebViewJavascriptBridge) ;
} ,
false
) ;
}
}
export default {
callhandler ( name, data, callback ) {
if ( / (iPhone|iPad|iPod|iOS) / i . test ( navigator. userAgent) ) {
connectWebViewJavascriptBridgeIOS ( function ( bridge ) {
bridge. callHandler ( name, data, callback)
} )
}
if ( / (Android) / i . test ( navigator. userAgent) ) {
connectWebViewJavascriptBridgeANDROID ( function ( bridge ) {
bridge. callHandler ( name, data, callback)
} )
}
} ,
registerhandler ( name, callback ) {
if ( / (iPhone|iPad|iPod|iOS) / i . test ( navigator. userAgent) ) {
connectWebViewJavascriptBridgeIOS ( function ( bridge ) {
bridge. registerHandler ( name, function ( data, responseCallback ) {
callback ( data, responseCallback)
} )
} )
}
if ( / (Android) / i . test ( navigator. userAgent) ) {
connectWebViewJavascriptBridgeANDROID ( function ( bridge ) {
bridge. init ( function ( message, responseCallback ) {
if ( responseCallback) {
}
} ) ;
bridge. registerHandler ( name, function ( data, responseCallback ) {
callback ( data, responseCallback)
} )
} )
}
} ,
}
main.js mount
import Bridge from "@/config/bridge.js"
Vue . prototype. $bridge = Bridge
use bridge
testWebViewBridge
It is the method originally declared by the app, and the function of the original app is called through the callhandler method in the bridge function of $bridge
Among them, action is the parameter that h5 needs to pass to the native app, and multiple parameters can be used
this.$bridge.callhandler('testWebViewBridge', { param1:param1,param2:param2 }, data => { console.log(data ) })
data is the function returned by the native app to h5 (which contains the status and results of the current call)
methods : {
goNative ( ) {
const that= this ;
this . $bridge. callhandler (
"testWebViewBridge" ,
{
action : "pick me" } ,
data => {
that. ddd = data;
}
) ;
} ,
iosToH5 ( ) {
} ,
} ,
mounted ( ) {
const that= this ;
this . $bridge. registerhandler ( "iosToH5" , ( data, responseCallback ) => {
that. native_data = data;
} ) ;
}