Kurs React Native · Moduł 13: Natywne API
expo-sms & expo-mail-composer - SMS i Email
W tej lekcji3
Wysyłaj wiadomości SMS i email!
expo-sms
npx expo install expo-smsimport * as SMS from 'expo-sms';
async function sendSMS(phoneNumber: string, message: string) {
const isAvailable = await SMS.isAvailableAsync();
if (!isAvailable) {
Alert.alert('SMS niedostępne');
return;
}
const { result } = await SMS.sendSMSAsync(
[phoneNumber], // Można podać wiele numerów
message
);
// result: 'sent', 'cancelled', lub 'unknown'
return result;
}
// Przykład użycia
await sendSMS('+48123456789', 'Witaj z Atlantydy!');expo-mail-composer
npx expo install expo-mail-composerimport * as MailComposer from 'expo-mail-composer';
async function sendEmail() {
const isAvailable = await MailComposer.isAvailableAsync();
if (!isAvailable) {
Alert.alert('Email niedostępny');
return;
}
const result = await MailComposer.composeAsync({
recipients: ['kontakt@atlantyda.app'],
ccRecipients: ['kopia@atlantyda.app'],
bccRecipients: ['ukryta@atlantyda.app'],
subject: 'Wiadomość z Atlantydy',
body: 'Witaj! To wiadomość wysłana z aplikacji Atlantyda.',
isHtml: false,
attachments: ['/path/to/file.pdf'], // Opcjonalne załączniki
});
// result.status: 'sent', 'saved', 'cancelled', 'undetermined'
return result;
}Serwis komunikacji
const CommunicationService = {
async sendSMS(phone: string, message: string) {
if (await SMS.isAvailableAsync()) {
return SMS.sendSMSAsync([phone], message);
}
throw new Error('SMS niedostępne');
},
async sendEmail(to: string, subject: string, body: string) {
if (await MailComposer.isAvailableAsync()) {
return MailComposer.composeAsync({
recipients: [to],
subject,
body,
});
}
throw new Error('Email niedostępny');
},
async call(phone: string) {
const url = `tel:${phone}`;
if (await Linking.canOpenURL(url)) {
await Linking.openURL(url);
}
},
};Kod do tej lekcji: App.tsx
import React from 'react';
import { View, Text, Button, StyleSheet, Alert } from 'react-native';
import * as SMS from 'expo-sms';
import * as MailComposer from 'expo-mail-composer';
import * as Linking from 'expo-linking';
// Serwis komunikacji wykorzystujący natywne funkcje telefonu
const CommunicationService = {
async sendSMS(phone: string, message: string) {
// TODO: sprawdź dostępność: if (await SMS.isAvailableAsync()) { ... }
// TODO: wewnątrz wyślij SMS: return SMS.sendSMSAsync([phone], message);
// TODO: w przeciwnym razie: throw new Error('SMS niedostępne');
},
async sendEmail(to: string, subject: string, body: string) {
// TODO: sprawdź dostępność: if (await MailComposer.isAvailableAsync()) { ... }
// TODO: wewnątrz wyślij email:
// return MailComposer.composeAsync({ recipients: [to], subject, body });
// TODO: w przeciwnym razie: throw new Error('Email niedostępny');
},
async call(phone: string) {
const url = `tel:${phone}`;
// TODO: sprawdź czy można otworzyć URL: if (await Linking.canOpenURL(url)) { ... }
// TODO: wewnątrz otwórz: await Linking.openURL(url);
},
};
export default function App() {
return (
<View style={styles.screen}>
<Text style={styles.title}>Komunikacja Atlantydy</Text>
<Button
title="Wyślij SMS"
onPress={() => CommunicationService.sendSMS('+48123456789', 'Witaj z Atlantydy!')}
/>
<Button
title="Wyślij email"
onPress={() => CommunicationService.sendEmail('kontakt@atlantyda.app', 'Cześć', 'Treść')}
/>
<Button
title="Zadzwoń"
onPress={() => CommunicationService.call('+48123456789')}
/>
</View>
);
}
const styles = StyleSheet.create({
screen: {
flex: 1,
justifyContent: 'center',
padding: 24,
gap: 12,
backgroundColor: '#1a5276',
},
title: {
fontSize: 22,
color: '#fff',
fontWeight: '600',
marginBottom: 16,
},
});Hasła z dokumentacji w tej lekcji
Widzisz błąd w tej lekcji?
Sprawdź się
Odpowiedz na pytania z tej lekcji. Wybierz odpowiedź, a od razu zobaczysz, czy jest poprawna.
1. Jak wysłać SMS?
2. Jak sprawdzić dostępność SMS?
Zadania praktyczne w grze
- Klikanie w kolejności
Kliknij elementy wysyłania email:
- Edytor kodu
Uzupełnij kod CommunicationService.