Skip to content

RN Lez 30: Share, Linking, Vibration

Docs: Share, Linking, Vibration

  • Condividere contenuti con Share API
  • Aprire link esterni con Linking
  • Usare Vibration per feedback tattile

import { Share } from 'react-native';
const condividi = async (messaggio: string) => {
try {
await Share.share({
message: messaggio, // testo da condividere
title: 'Guarda questo!', // solo Android
});
} catch (err) {
console.error(err);
}
};
<TouchableOpacity onPress={() => condividi(`"${citazione}" — ${autore}`)}>
<Text>📤 Condividi</Text>
</TouchableOpacity>

import { Linking } from 'react-native';
// Aprire sito web
const apriURL = async (url: string) => {
const supportato = await Linking.canOpenURL(url);
if (supportato) {
await Linking.openURL(url);
} else {
Alert.alert('Errore', `Impossibile aprire: ${url}`);
}
};
// Aprire email
const apriEmail = (email: string) => {
Linking.openURL(`mailto:${email}`);
};
// Aprire telefono
const chiama = (numero: string) => {
Linking.openURL(`tel:${numero}`);
};
// Aprire mappe (Android)
const apriMappe = (lat: number, lng: number) => {
const url = Platform.select({
ios: `maps://?q=${lat},${lng}`,
android: `geo:${lat},${lng}?q=${lat},${lng}`,
});
Linking.openURL(url!);
};

import { Vibration } from 'react-native';
// Vibrazione singola (400ms)
Vibration.vibrate(400);
// Pattern: vibra 200ms, ferma 100ms, vibra 200ms
Vibration.vibrate([200, 100, 200], false);
// ↑ false = non ripetere
// Fermare vibrazione
Vibration.cancel();

4. BackHandler — gestire tasto indietro (Android)

Section titled “4. BackHandler — gestire tasto indietro (Android)”
import { BackHandler } from 'react-native';
import { useEffect } from 'react';
useEffect(() => {
const backHandler = BackHandler.addEventListener('hardwareBackPress', () => {
Alert.alert('Uscire?', 'Vuoi chiudere l\'app?', [
{ text: 'No', style: 'cancel' },
{ text: '', onPress: () => BackHandler.exitApp() },
]);
return true; // ← blocca comportamento default
});
return () => backHandler.remove(); // cleanup
}, []);

APICodice
ShareShare.share({ message: '...' })
Linking URLLinking.openURL(url)
Linking canOpenLinking.canOpenURL(url)
VibrationVibration.vibrate(ms)
BackHandlerBackHandler.addEventListener(...)

Crea un’app che:

  1. Mostra un contatto (nome, email, telefono)
  2. Bottone “Chiama” → apre telefono
  3. Bottone “Email” → apre mail
  4. Bottone “Sito web” → apre browser
  5. Bottone “Condividi” → condivide contatto
  6. Bottone vibrazione lunga (500ms)
  7. Gestione BackHandler su Android con conferma