美文网首页
taro H5发短信

taro H5发短信

作者: ai耳边的呢喃 | 来源:发表于2020-08-04 14:14 被阅读0次

    1、判断环境,安卓和 iOS

    const u = navigator.userAgent;
    const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    

    2、调用短信功能

        let smsContent = '';
        if (isAndroid) {
          smsContent = `sms:13838883888?body=您好`
        } else if(isiOS){
          smsContent = `sms:13838883888&body=您好`
        }
    

    3、利用 web-view嵌入网页, 再通过网页调用短信功能

    完整代码

    import Taro , { Component } from '@tarojs/taro';
    import { View, WebView} from '@tarojs/components';
    export default class SendMessage extends Component {
      constructor(props) {
        super(props);
        this.state = {
          isWebView: false,
          smsContent: '',
        }
      }
       // 发短信
      onSendAMsg() {
        let smsContent = ''
        const u = navigator.userAgent;
        const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
        const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
        if (isAndroid) {
          smsContent = `sms:13838883888?body=您好`
        } else if(isiOS){
          smsContent = `sms:13838883888&body=您好`
        }
        this.setState({ isWebView: true, smsContent })
        // isWebView作为 web-view 的状态控制,如果没有,进页面时就会调用短信功能
        // 页面会保存 web-view 的打开状态,所以要把 web-view 的状态关闭
        const timer = setTimeout(() => {
          this.setState({ isWebView: false });
          clearTimeout(timer)
        }, 1000)
      }
     render() {
        const { isWebView, smsContent } = this.state
        return (
          <View className='phone_btn sms' onClick={this.onSendAMsg.bind(this)}>发送短信</View>
          {isWebView && <WebView src={smsContent}></WebView>}
        )
     }
    }
    
    

    相关文章

      网友评论

          本文标题:taro H5发短信

          本文链接:https://www.haomeiwen.com/subject/zmhwrktx.html