当前位置:
首页
文章
前端
详情

React Native拨打电话

Linking 提供了一个通用的接口来与传入和传出的 App 链接进行交互。

最常用的方案:

  • mailto
  • tel
  • sms(短信服务Short Messaging Service)
  • https / http
import React, { PureComponent } from 'react';
import { Text, TouchableOpacity, Linking, Alert } from 'react-native';
import { Toast } from '@ant-design/react-native';

class Contact extends PureComponent {
  /**
   *  拨打电话
   * @param {string} phone 版本号
   * @example
   * call('18888888888')
   */
  call = phone => {
    const url = `tel:${phone}`;
    Linking.canOpenURL(url)
      .then(supported => {
        if (!supported) {
          return Alert.alert('提示', `您的设备不支持该功能,请手动拨打 ${phone}`, [
            { text: '确定' }
          ]);
        }
        return Linking.openURL(url);
      })
      .catch(err => Toast.info(`出错了:${err}`, 1.5));
  };

  callMerchant = () => {
    this.call('18888888888');
  };

  render() {
    return (
      <TouchableOpacity onPress={this.callMerchant}>
        <Text>联系商家</Text>
      </TouchableOpacity>
    );
  }
}

export default Contact;

免责申明:本站发布的内容(图片、视频和文字)以转载和分享为主,文章观点不代表本站立场,如涉及侵权请联系站长邮箱:xbc-online@qq.com进行反馈,一经查实,将立刻删除涉嫌侵权内容。