h5页面与android(WebViewJavascriptBridge) / ios(window.webkit.messageHandlers)交互

一、首先是h5页面与android交互:这里,我交互的android用的是WebViewJavascriptBridge:

android调用h5方法并传递消息:

注意:在vue与android交互的时候,android的方法里this指向无效,需要赋值that;

  h5传递消息给android:

二、h5页面与IOS交互,这里IOS用的是window.webkit.messageHandlers:

 IOS调用h5页面方法,并传递消息:

写一个方法,接受对应的参数,此时不需要调用这个方法,只需要把这个方法暴露给window,IOS便可直接获取到,进行传参;h5只要在方法内接收对应参数进行操作即可

h5页面发送消息给IOS

定义好需要传的内容,window.webkit这句是关键,可直接把dict定义好的内容发送给ios接收,FirstJsObect2是需要在ios内注册的一个方法名;
 

``` // import instance from "../http/mhttp"; import cookie from "js-cookie"; import router from "../router"; // import md5 from 'js-md5' export default { //獲取cookies值 // (详情页使用沉浸式) getCookie(name) { let strcookie = document.cookie; let arrcookie = strcookie.split("; "); for (let i = 0; i < arrcookie.length; i++) { let arr = arrcookie[i].split("="); if (arr[0] == name) return unescape(arr[1]); } return null; }, //獲取客戶端 client() { return this.getCookie("odrplatform") || ""; }, translateStatusBar(status_true) { let odrplatform = cookie.get("odrplatform"); if (odrplatform == "android") { window.apps.translateStatusBar(status_true); } else if (odrplatform == "ios") { window.webkit.messageHandlers.translateStatusBar.postMessage(`${status_true}`); } }, //去原生收银台 0 gotoPay(out_trade_no, money) { // let client = this.getCookie('odrplatform');//客户端 if (this.client() == "android") { window.apps.gotoPay(out_trade_no, money); } else if (this.client() == "ios") { window.webkit.messageHandlers.gotoPay.postMessage(`${out_trade_no},${money}`); } else { // console.log('Unable come back'); router.go(-1); } }, //去原生收银台 (积分) gotoPay2(out_trade_no, money,type,point) { // let client = this.getCookie('odrplatform');//客户端 if (this.client() == "android") { window.apps.gotoPay2(out_trade_no, money,type,point); } else if (this.client() == "ios") { window.webkit.messageHandlers.gotoPay.postMessage(`${out_trade_no},${money}`); } else { // console.log('Unable come back'); router.go(-1); } }, //去原生收银台 1 gotoNodePay(out_trade_no, money, number, is_rice_wine,shipping_fee) { // let client = this.getCookie('odrplatform');//客户端 if (this.client() == "android") { window.apps.gotoNodePay(out_trade_no, money, number, is_rice_wine, shipping_fee); } else if (this.client() == "ios") { window.webkit.messageHandlers.gotoNodePay.postMessage(`${out_trade_no},${money},${number},${is_rice_wine}, ${shipping_fee}`); } else { // console.log('Unable come back'); router.go(-1); } }, //退出h5頁面 exit() { // let client = this.getCookie('odrplatform');//客户端 if (this.client() == "android") { window.apps.finish(); } else if (this.client() == "ios") { window.webkit.messageHandlers.iosAction.postMessage("exit"); } else { // console.log('Unable come back'); router.go(-1); } }, //返回或者退出h5頁面 goback() { // let client = this.getCookie('odrplatform');//客户端 if (this.client() == "android") { window.apps.goback(); } else if (this.client() == "ios") { window.webkit.messageHandlers.iosAction.postMessage("goBack"); } else { // console.log('Unable come back'); } }, //去登錄 gologin() { // let client = this.getCookie('odrplatform');//客户端 if (this.client() == "android") { window.apps.gologin(); } else if (this.client() == "ios") { window.webkit.messageHandlers.iosAction.postMessage("login"); } }, //跳原生购物车 goShoppingCart() { let odrplatform = cookie.get("odrplatform"); if (odrplatform == "android") { window.apps.goShoppingCart(); } else if (odrplatform == "ios") { window.webkit.messageHandlers.goShoppingCart.postMessage({}); } }, };```uni需要打包成h5
最新发布
03-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值