react-native - 反应原生的电话
问题描述
我想在 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>
);
}
导出默认联系人;
解决方案
如果你有一个 phoneNumber 变量,看起来你没有为你的 phoneNumber 字符串使用反引号。现在您正在对数字进行硬编码,因此只需使用tel:12345678
不带大括号和美元符号的数字即可。此外,tel:${number}
如果您使用较新的 react-native 版本,应该在两个平台上都可以使用。请记住,iOS模拟器不支持打开呼叫,因此您必须在真实设备或Android上进行测试。
推荐阅读
- sql-server - 带有单个单词的 SQL Contains() 不会返回所有预期的行
- javascript - VueJS 阻止在每个 v-for 循环中显示数据
- javascript - DataTable 未格式化日期时间
- java - 在日期和时间之间用“T”解析时间戳
- python - 如何检查熊猫行中的连续差异
- java - 如何在java中转换泛型方法的返回?
- powershell - CIM、Win32_OperatingSystem 和 Win32ShutdownTracker
- oop - 从 OOP 的角度来看,在接口中声明类型/属性是否正确?
- ionic4 - 是否可以在离子选择器中自动选择并立即跳转到某个值?
- r - 将 R 中的多个 RDS 文件作为自定义函数中的多个对象加载