vue ios call the method provided

After a variety of Baidu, saw an article in another station, you can use WebViewJavascriptBridge introduced with the native app to communicate;

I tried it in the project, recording what really works.

Original link:

https://blog.csdn.net/szjSmiling/article/details/82773293

1, the introduction of third-party libraries WebViewJavascriptBridge

 1     function setupWebViewJavascriptBridge(callback) {
 2       if (window.WebViewJavascriptBridge) {
 3         return callback(window.WebViewJavascriptBridge)
 4       }
 5       if (window.WVJBCallbacks) {
 6         return window.WVJBCallbacks.push(callback)
 7       }
 8       window.WVJBCallbacks = [callback]
 9       let WVJBIframe = document.createElement('iframe');
10       WVJBIframe.style.display = 'none'
11       WVJBIframe.src = 'https://__bridge_loaded__'
12       document.documentElement.appendChild(WVJBIframe);
13       setTimeout(() => {
14         document.documentElement.removeChild(WVJBIframe)
15       }, 0)
16     }
17     export default {
18       callhandler(name, data, callback) {
19         setupWebViewJavascriptBridge(function (bridge) {
20           bridge.callHandler(name, data, callback)
21         })
22       },
23       registerhandler(name, callback) {
24         setupWebViewJavascriptBridge(function (bridge) {
25           bridge.registerHandler(name, function (data, responseCallback) {
26             callback(data, responseCallback)
27           })
28         })
29       }
30     }

Create a js file, copy and paste the above code like

2, the introduction of paper main.js

import Bridge from './assets/js/bridge.js'

Vue.prototype.$bridge = Bridge

3, call the method

UA = the let the navigator.userAgent; 
            the let isAndroid = ua.indexOf ( 'the Android')> -1 || ua.indexOf ( 'Adr')> -1 ; 
            the let isiOS = !! ua.match (/ \ (I [^ ;] +; (the U-;)?. the CPU the Mac the OS X-+ / );
             // the console.log (isAndroid, 'Andrews') 
            // the console.log (isiOS, 'iOS') 
            IF (isiOS) { 

                the this $. bridge.callhandler ( 'popToStudyRootVC', (data) => {
                     // returns the process data 
                    console.log (data, 'I go here' ) 
                }) 
            }
Method name is defined colleagues have developed a native of: popToStudyRootVC

This is done

 

Guess you like

Origin www.cnblogs.com/yutianA/p/11200768.html