131

I want to call to the value of Text component when I press it. But, actually, I haven't enough knowledge for that.

Can you, please, tell me, which library or component should I use?

enter image description here enter image description here

TylerH
  • 20,799
  • 66
  • 75
  • 101
Just Ahead
  • 2,377
  • 6
  • 16
  • 25

11 Answers11

339

If you look at the source code for react-native-phone-call, it's ultimately just a wrapper for:

import {Linking} from 'react-native'
Linking.openURL(`tel:${phoneNumber}`)
Tim
  • 6,265
  • 5
  • 29
  • 24
71

You can use this method to call numbers in android and ios, place this method in a utils file and use the method wherever you want. cheers

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

**Update ( Andrey Patseiko's comment) **

Don't forget added to Info.plist ->

<key>LSApplicationQueriesSchemes</key> 
<array> 
  <string>tel</string> 
  <string>telprompt</string> 
</array>
CanCoder
  • 1,073
  • 14
  • 20
Aakash Daga
  • 1,433
  • 10
  • 7
  • 10
    And don't forget added to Info.plist -> LSApplicationQueriesSchemes tel telprompt – Andrey Patseiko Oct 01 '19 at 09:23
  • Thank you very much for the complete solution, and also thanks for @AndreyPatseiko for explaining about the Info.plist key. This is answer is way more complete than the one tagged as the "best answer" – Gabcvit Oct 23 '19 at 09:33
  • just do what tim said – ICW Jul 06 '22 at 13:53
31

it is very simple for ios:

import {Linking} from 'react-native'

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

hook zou
  • 581
  • 5
  • 6
13
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>
              )
  }

}
ramashish tomar
  • 815
  • 11
  • 35
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>
Sourabh Gera
  • 862
  • 7
  • 7
7

Simply onPress action with {Linking.openURL(tel:${phonenumber});} can be put

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

ps:Dont forget to import Linking from 'react-native'

Sjonchhe
  • 790
  • 8
  • 16
6

The simplest way of achieving this is like this

import { Linking } from "react-native";

Linking.openURL(`tel:${phoneNumber}`);
AngelNext
  • 158
  • 2
  • 9
6
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>
  );
}

Just import Linking from react native and add this {Linking.openURL('tel:9873');}in onPress function. You can replace 9873 with any number you want.

UMANG GUPTA
  • 61
  • 1
  • 1
5

1. install react-native-phone-call package using npm

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

2. create a method called 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. call the method in onPress event of a TouchableOpacity

<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)}
>
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
2

An example:

<ButtonOrange onPress={() => { Linking.openURL(`tel:999990000`) }}>
    <ViewCenter>
        <Icon name="phone" size={18} color="#fff" />
        <ButtonText>Call</ButtonText>
    </ViewCenter>
</ButtonOrange>
cokeman19
  • 2,405
  • 1
  • 25
  • 40
Paulinho
  • 21
  • 2
0

On android, react-native-send-intent is great to dial a phone number without opening android dialer app.

On iOS, use the openUrl method which do (almost) the same.

Massale
  • 56
  • 1
  • 9