js调用android.webkit,h5调用原生App的方法合集 window.webkit.messageHandlers

这篇博客展示了如何使用`navigator.userAgent`检查平台,并通过`window.webkit.messageHandlers`调用Android和iOS原生App的功能,如关闭、打电话、跳转和分享图片到微信。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

测试demo

调用原生关闭函数

let u = navigator.userAgent

let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //g

let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端

if (isAndroid) {

jsObj.webClose(); // 调用原生app的函数

}

if (isIOS) {

window.webkit.messageHandlers.webClose.postMessage();

}

调用原生打电话

let u = navigator.userAgent

let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //g

let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端

let phone = app._data.detail.driver_mobile

if (isAndroid) {

jsObj.callPhone(JSON.stringify(phone));

}

if (isIOS) {

window.webkit.messageHandlers.callPhone.postMessage(JSON.stringify(phone))

``` // 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、付费专栏及课程。

余额充值