import { Linking, Alert } from 'react-native'; import Clipboard from '@react-native-clipboard/clipboard'; export const handleGoWhatsApp = async () => { const phoneWithCountryCode = '85291382139'; // 不带 "+",如 +60 改成 60(马来西亚) const message = ''; 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' } ]); } };