index.ts 514 B

123456789101112131415
  1. import { Linking, Alert } from 'react-native';
  2. export const handleGoWhatsApp = async () => {
  3. const phoneWithCountryCode = '8528691382139'; // 不带 "+",如 +60 改成 60(马来西亚)
  4. const message = 'Hello!';
  5. const url = `whatsapp://send?phone=${phoneWithCountryCode}&text=${encodeURIComponent(message)}`;
  6. const supported = await Linking.canOpenURL(url);
  7. if (supported) {
  8. await Linking.openURL(url);
  9. } else {
  10. Alert.alert('WhatsApp 未安裝', '請先安裝 WhatsApp 應用');
  11. }
  12. };