Requirements: Open WebView in native app and embed H5. Click on an element in the H5 to trigger the interaction with the native app and jump back into the app. Similarly, after doing something in the app, get a parameter and refresh the page based on that state.
Frame: Vue. The JavaScript native notation has been tuned and has been passed with the native end. So here's how to move them to the Vue framework.
This is to differentiate between iOS and Android systems.
iOS system
- After consulting with an iOS-developed colleague, I decided to use Webviewjavascriptbridge to develop it. Front end does not need to put any JS plugin. Just have to prepare this section.
Bridge.js
function Setupwebviewjavascriptbridge (callback) {if (window. Webviewjavascriptbridge) {return callback (window. Webviewjavascriptbridge)} if (window. Wvjbcallbacks) {Returnwindow. Wvjbcallbacks.push (Callback)} window. Wvjbcallbacks = [callback] Let Wvjbiframe = document.createelement (' iframe ') WVJBIframe.style.display = ' None ' Wvjbifra ME.SRC = ' https://__bridge_loaded__ ' Document.documentElement.appendChild (wvjbiframe) setTimeout (() = {document. Documentelement.removechild (Wvjbiframe)}, 0)}export default {callhandlerios (name, data, callback) {SETUPWEBVIEWJ Avascriptbridge (bridge) {Bridge.callhandler (name, data, Callback)})}, Registerhandlerios (name , callback) {Setupwebviewjavascriptbridge (bridge) {Bridge.registerhandler (name, function (data, RE Sponsecallback) {Callback (data, Responsecallback)})}}}
- Injected into the main.js.
import Bridge from "./js/bridge.js";Vue.prototype.$bridge = Bridge //在这里注入后, 页面里所有需要用到调用native的地方, 都可以直接调用this.$bridge.xxxnew Vue({ el: "#app", router, store, // Bridge, components: { App }, template: "<App/>",})
- Use in files to be used
.vue
Give an example here.
H5 calling iOS
callNativeused in methodsthis.$bridge.callhandler(‘ObjC Echo‘, params, function(response){//todo})
ObjC EchoThis is the method that is agreed with the client. That is, in the client registration good method, write the method name, in the use of the client can receive, so that the parameters in the params, and followed by the function(response){} processing of response data. Do something.
this.$bridge.callhandlerIOS( "HY_H5_CALL_NATIVE", { action: "pick me" }, data => { // 处理返回数据 })
iOS call H5
Usethis.$bridge.registerhandler(‘JS Echo‘, (data, responseCallback)=>{//todo}
JS EchoThat is, the method name is agreed with the client, and when the client uses the method name, it automatically H5 to perform certain operations. The message from the client is put in data , responseCallback that is, the callback is executed.
this.$bridge.registerhandlerIOS("JS Echo", (data, responseCallback) => { alert("JS Echo called with:", data); // this.responseCallback(data)});
Android system
As the Android system also uses Webviewjavascriptbridge, I naturally think about whether I can also export two processing handler like iOS.
- Adding a claim code in Bridge.js
//Android 交互声明function connectWebViewJavascriptBridgeANDROID(callback) { if (window.WebViewJavascriptBridge) { callback(WebViewJavascriptBridge); } else { document.addEventListener( "WebViewJavascriptBridgeReady", function () { callback(WebViewJavascriptBridge); }, false ); }}
- Add an export in the bridge.js export
Here I have added Android as the handler of iOS to make a distinction behind handler.
callhandlerAndroid(name, data, callback){ //Android方法 connectWebViewJavascriptBridgeANDROID(function(bridge){ bridge.callHandler(name, data, callback) }) }, registerhandlerAndroid(name, callback){ //Android方法 connectWebViewJavascriptBridgeANDROID(function(bridge){ bridge.init(function(message, responseCallback) { if (responseCallback) { // responseCallback(data); } }); bridge.registerHandler(name, function(data, responseCallback){ callback(data, responseCallback) }) }) }
.vueused in files. How to use with iOS
JS Call Android
this.$bridge.callhandlerAndroid( ‘action‘, {message: ‘111‘}, function(response){})
Android Call JS
this.$bridge.registerhandlerAndroid( "registerAction", (message, responseCallback) => { alert("JS Echo called with:" + message); // this.responseCallback(data) });
At this point, JS and Android, the iOS side even tuned.
I feel the Android this piece still has a lot to optimize the place, but my JS also mastered not very solid ... So let's do it first. After I have a new feeling, and then to update.
Attach All Codes
- Bridge.js Code
IOS Interactive declares function Connectwebviewjavascriptbridgeios (callback) {if (window. Webviewjavascriptbridge) {return callback (window. Webviewjavascriptbridge)} if (window. Wvjbcallbacks) {Returnwindow. Wvjbcallbacks.push (Callback)} window. Wvjbcallbacks = [callback] Let Wvjbiframe = document.createelement (' iframe ') WVJBIframe.style.display = ' None ' Wvjbifra ME.SRC = ' https://__bridge_loaded__ ' Document.documentElement.appendChild (wvjbiframe) setTimeout (() = {document. Documentelement.removechild (Wvjbiframe)}, 0)}//android Interactive declaration function connectwebviewjavascriptbridgeandroid ( Callback) {if (window. Webviewjavascriptbridge) {callback (Webviewjavascriptbridge); } else {Document.addeventlistener ("Webviewjavascriptbridgeready", function () {Callback (Webviewjava Scriptbridge); }, False); }}export Default {Callhandlerios (name, data, callback) {//ios method Connectwebviewjavascriptbridgeios (function (bridge) {Bridge.callhandler (NamE, data, Callback)})}, Registerhandlerios (name, callback) {//ios method Connectwebviewjavascriptbridgeios (function (bridge) {Bridge.registerhandler (name, function (data, Responsecallback) {callback (data, Responsecallback)}) })}, Callhandlerandroid (name, data, callback) {//android method connectwebviewjavascriptbridgeandroid (function (bridge) { Bridge.callhandler (name, data, Callback)})}, Registerhandlerandroid (name, callback) {//android method connectw Ebviewjavascriptbridgeandroid (function (bridge) {Bridge.init (function (message, responsecallback) {if (response Callback) {//Responsecallback (data); } }); Bridge.registerhandler (name, function (data, Responsecallback) {callback (data, Responsecallback)})}}}
- Main.js Code
import Bridge from "./js/bridge.js";Vue.prototype.$bridge = Bridge/* eslint-disable no-new */new Vue({ el: "#app", router, store, components: { App }, template: "<App/>",});
.vueFile
<template> <div>
The interaction of JS with native in the Vue frame (ios&android)