How to make phone call in React Native?
it is very simple for ios:
import {Linking} from 'react-native'
<Text onPress={()=>{Linking.openURL('tel:119');}} style={styles.funcNavText}>119</Text>
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>
)
}
}
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>
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}`)