如何在React Native中拨打电话?

问题描述 投票:17回答:5

我想按下它时调用Text组件的值。但是,实际上,我对此知之甚少。

请你告诉我,我应该使用哪个库或组件?

enter image description here enter image description here

react-native phone-call
5个回答
89
投票

如果你看一下react-native-phone-call的源代码,它最终只是一个包装器:

import {Linking} from 'react-native'
Linking.openURL(`tel:${phoneNumber}`)

13
投票

您可以使用此方法在android和ios中调用数字,将此方法放在utils文件中,并在任意位置使用该方法。干杯

import { Linking, Alert, Platform } from 'react-native';



export const callNumber = phone => {
console.log('callNumber ----> ', phone);
let phoneNumber = phone;
if (Platform.OS !== 'android') {
phoneNumber = `telprompt:${phone}`;
}
else  {
phoneNumber = `tel:${phone}`;
}
Linking.canOpenURL(phoneNumber)
.then(supported => {
if (!supported) {
    Alert.alert('Phone number is not available');
  } else {
    return Linking.openURL(phoneNumber);
}
})
.catch(err => console.log(err));
};

5
投票

对于ios来说非常简单:

import {Linking} from 'react-native'

<Text onPress={()=>{Linking.openURL('tel:119');}} style={styles.funcNavText}>119</Text>


4
投票

不是真正的答案,但我没有足够的代表评论。

怎么样react-native-phone-callreact-native-communications


1
投票

1.使用npm安装react-native-phone-call包

$ npm install --save react-native-phone-call

2.创建一个名为makeCall()的方法

makeCall = (number) => {
     const args = {
         number: number, // String value with the number to call
         prompt: true // Optional boolean property. Determines if the user should be prompt prior to the call 
     }
    call(args).catch(console.error)
}

3.在TouchableOpacity的onPress事件中调用该方法

<TouchableOpacity key={index} style={{width: '80%', height: 80, backgroundColor: 'rgb(186, 186, 186)',  alignItems:'center', justifyContent: 'space-between', borderBottomWidth: 0.5, borderBottomColor: '#000000'}}
             onPress={()=> this.makeCall(item.contactNumber)}
>

0
投票

只需使用{Linking.openURL(tel:${phonenumber});} onPress动作即可

<Text onPress={()=>{Linking.openURL('tel:8777111223');} }>8777111223</Text>

ps:别忘了从'react-native'导入链接


0
投票
import React, { Component } from "react";
import {Linking,Platform,TouchableOpacity,Text} from "react-native";
export default class MakeCall extends Component {

 dialCall = (number) => {
    let phoneNumber = '';
    if (Platform.OS === 'android') { phoneNumber = `tel:${number}`; }
    else {phoneNumber = `telprompt:${number}`; }
    Linking.openURL(phoneNumber);
 };

 Render(){
        return(
                <TouchableOpacity
                   style={{
                   height: 30,
                   width: 30,
                   backgroundColor: "#329df4",
                   alignItems: "center",
                   justifyContent: "center",
                   borderRadius: 5
                   }}
                 onPress={()=>{this.dialCall(123456789)}}
                >
                <Text>Phone</Text>
                </TouchableOpacity>
              )
  }

}
© www.soinside.com 2019 - 2024. All rights reserved.