index.ts 1.1 KB

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