首页 > 解决方案 > 反应原生的电话

问题描述

我想在 React Native 中打电话,我是 React Native 的初学者,所以我遇到了这个看起来像我的问题 =>如何在 React Native 中打电话?

所以,这是我的代码的一部分,但它不起作用,当我点击图标时,什么也没有发生,我的终端上也没有错误,这很奇怪。这是我的代码:

import React from 'react';
import { Image, Text, View, StyleSheet } from 'react-native';

import { Avatar } from "react-native-elements";
import { Linking } from 'react-native';

export const makeCall = () => {

    let phoneNumber = '';

    if (Platform.OS === 'android') {
        phoneNumber = 'tel:${0123456789}';
    } else {
        phoneNumber = 'telprompt:${0123456789}';
    }

    Linking.openURL(phoneNumber);
};

const Contacts = () => {

  return (
    <View style={styles.column}>
       <Avatar
         size={65}
         rounded
         overlayContainerStyle={{ backgroundColor: '#fff' }}
         icon={{ name: 'phone', color: '#113D78', type: 'font-awesome' }}
         onPress={() => makeCall}
         style={{
           width: 65,
           height: 65,
           borderRadius: 50,
           borderWidth: 2,
           borderColor: '#113D78',
         }}
     />
     <Text style={styles.subtitle}>Phone</Text>
   </View>
);

}

导出默认联系人;

标签: react-nativephone-call

解决方案


如果你有一个 phoneNumber 变量,看起来你没有为你的 phoneNumber 字符串使用反引号。现在您正在对数字进行硬编码,因此只需使用tel:12345678不带大括号和美元符号的数字即可。此外,tel:${number}如果您使用较新的 react-native 版本,应该在两个平台上都可以使用。请记住,iOS模拟器不支持打开呼叫,因此您必须在真实设备或Android上进行测试。


推荐阅读