vue移动端H5调起手机发送短信(兼容ios和android)

移动端h6页面调起手机发送短信功能,ios和android的兼容写法不一样。

android

TypeScript 复制代码
window.location.href = `sms:10086?body=${encodeURIComponent('Hello, 测试短信发送')}`

ios

TypeScript 复制代码
window.location.href = `sms:10086&body=${encodeURIComponent('Hello, 测试短信发送')}`

//或者

window.location.href =`sms:/open?addresses=10086&body=${encodeURIComponent('Hello, 测试短信发送')}`;
html 复制代码
<template>
   <view @click="openNewSmsPage"></view>
</template>
TypeScript 复制代码
<script lang="ts" setup>
//点击唤起短信发送
const openNewSmsPage = () => {
  const phoneNumber = '10086' // 目标手机号码
  const message = 'Hello, 测试短信发送' // 短信内容
  let smsLink = ''
  const u = navigator.userAgent,
    isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1,
    isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
  if (isIOS) {
    // window.location.href = 'sms:10086&body=' + msg
    smsLink = `sms:${phoneNumber}&body=${encodeURIComponent(message)}`
    //或者
    smsLink = `sms:/open?addresses=${phoneNumber}&body=${encodeURIComponent(message)}`;
  } else {
    // sms:后面跟收件人的手机号,body后接短信内容
    smsLink = `sms:${phoneNumber}?body=${encodeURIComponent(message)}`
  }
  window.location.href = smsLink
}
</script>
相关推荐
源码获取_wx:Fegn08951 小时前
基于springboot + vue心理健康管理系统
vue.js·spring boot·后端
韩立学长1 小时前
【开题答辩实录分享】以《基于Vue的非遗文化知识分享平台的设计与实现》为例进行选题答辩实录分享
前端·javascript·vue.js
优弧1 小时前
离开舒适区100天,我后悔了吗?
前端·后端·面试
胡gh2 小时前
css的臂膀,前端动效的利器,还是布局的“隐形陷阱”?
前端·css·html
灵感菇_2 小时前
Flutter Riverpod 完整教程:从入门到实战
前端·flutter·ui·状态管理
用户21411832636022 小时前
紧急修复!Dify CVE-2025-55182 高危漏洞,手把手教你升级避坑
前端
前端 贾公子2 小时前
Vue响应式原理学习:基本原理
javascript·vue.js·学习
Vic101012 小时前
解决 Spring Security 在异步线程中用户信息丢失的问题
java·前端·spring
源码获取_wx:Fegn08953 小时前
基于springboot + vue二手交易管理系统
java·vue.js·spring boot·后端·spring·课程设计
wordbaby3 小时前
Expo (React Native) 最佳实践:TanStack Query 深度集成指南
前端·react native