如何在React Native中拨打电话?

Jus*_*ead 33 phone-call react-native

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

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

在此输入图像描述 在此输入图像描述

Tim*_*Tim 108

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

import {Linking} from 'react-native'
Linking.openURL(`tel:${phoneNumber}`)
Run Code Online (Sandbox Code Playgroud)


小智 16

您可以使用此方法在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));
};
Run Code Online (Sandbox Code Playgroud)

  • 并且不要忘记添加到 Info.plist -> <key>LSApplicationQueriesSchemes</key> <array> <string>tel</string> <string>telprompt</string> </array> (5认同)

ram*_*mar 8

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>
              )
  }

}
Run Code Online (Sandbox Code Playgroud)


Sou*_*era 8

import { View,Linking,Text, Image,Platform,TouchableOpacity } from 'react-native';

 const onPressMobileNumberClick = (number) => {

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

 <TouchableOpacity 
       onPress={() => { onPressMobileNumberClick("9211886204") }} >
       <Text style={{ textDecorationLine: 'underline', textAlign: 'center', }>
               {"9211886204"}
       </Text>
   </TouchableOpacity>
Run Code Online (Sandbox Code Playgroud)


小智 8

import {useNavigation} from '@react-navigation/native';
import React from 'react';
import {Linking,View, Button} from 'react-native';

export function LegalScreen() {
  const navigation = useNavigation();

  return (
    <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
      <Button
        onPress={() => {Linking.openURL('tel:9873');}}
        title="Call Helpline"
      />
    </View>
  );
}
Run Code Online (Sandbox Code Playgroud)

只需从 React Native 导入 Linking 并在 onPress 函数中添加此 {Linking.openURL('tel:9873');} 即可。您可以将 9873 替换为您想要的任何号码。


小智 6

对于ios非常简单:

import {Linking} from 'react-native'

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

Run Code Online (Sandbox Code Playgroud)

  • 很好的答案。适用于 iOS 和 Android。无需从react-native导入Platform。 (2认同)

Ang*_*ext 6

实现这一目标的最简单方法是这样的

import { Linking } from "react-native";

Linking.openURL(`tel:${phoneNumber}`);
Run Code Online (Sandbox Code Playgroud)


Mar*_*rco 5

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

什么反应本地电话呼叫反应本土的通信


Cod*_*ker 5

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

$ npm install --save react-native-phone-call
Run Code Online (Sandbox Code Playgroud)

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)
}
Run Code Online (Sandbox Code Playgroud)

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)}
>
Run Code Online (Sandbox Code Playgroud)


小智 5

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

<Text onPress={()=>{Linking.openURL('tel:8777111223');} }>8777111223</Text>
Run Code Online (Sandbox Code Playgroud)

ps:不要忘记从'react-native'导入链接