index.ts 1.1 KB

123456789101112131415161718192021222324252627
  1. import { Linking, Alert } from 'react-native';
  2. import * as Clipboard from 'expo-clipboard';
  3. export const handleGoWhatsApp = async () => {
  4. const phoneWithCountryCode = '85291382139'; // 不带 "+",如 +60 改成 60(马来西亚)
  5. const message = '';
  6. const whatsappUrl = `whatsapp://send?phone=${phoneWithCountryCode}&text=${encodeURIComponent(message)}`;
  7. const webWhatsappUrl = `https://wa.me/${phoneWithCountryCode}`;
  8. try {
  9. const supported = await Linking.canOpenURL(whatsappUrl);
  10. if (supported) {
  11. await Linking.openURL(whatsappUrl);
  12. } else {
  13. // If WhatsApp app URL fails, try the web URL
  14. await Linking.openURL(webWhatsappUrl);
  15. Alert.alert('WhatsApp 未安裝', '請先安裝 WhatsApp 應用');
  16. }
  17. } catch (error) {
  18. console.error('Error opening WhatsApp:', error);
  19. // Fallback to showing the number if both methods fail
  20. Alert.alert('聯絡我們', `請通過WhatsApp聯繫我們:${phoneWithCountryCode}`, [
  21. { text: '複製號碼', onPress: async () => await Clipboard.setStringAsync(phoneWithCountryCode) },
  22. { text: '取消', style: 'cancel' }
  23. ]);
  24. }
  25. };