| 123456789101112131415161718192021222324252627 |
- import { Linking, Alert } from 'react-native';
- export const handleGoWhatsApp = async () => {
- const phoneWithCountryCode = '85291382139'; // 不带 "+",如 +60 改成 60(马来西亚)
- const message = null;
- const whatsappUrl = `whatsapp://send?phone=${phoneWithCountryCode}&text=${encodeURIComponent(message)}`;
- const webWhatsappUrl = `https://wa.me/${phoneWithCountryCode}`;
- try {
- const supported = await Linking.canOpenURL(whatsappUrl);
- if (supported) {
- await Linking.openURL(whatsappUrl);
- } else {
- // If WhatsApp app URL fails, try the web URL
- await Linking.openURL(webWhatsappUrl);
- Alert.alert('WhatsApp 未安裝', '請先安裝 WhatsApp 應用');
- }
- } catch (error) {
- console.error('Error opening WhatsApp:', error);
- // Fallback to showing the number if both methods fail
- Alert.alert('聯絡我們', `請通過WhatsApp聯繫我們:${phoneWithCountryCode}`, [
- { text: '複製號碼', onPress: () => Clipboard.setString(phoneWithCountryCode) },
- { text: '取消', style: 'cancel' }
- ]);
- }
- };
|