@sourceship13/react-native-share-sms
v0.1.6
Published
This package allows a user to share a message thru using the share menu or thru directly sending a sms
Readme
@sourceship13/react-native-share-sms
A React Native library for sending SMS messages and sharing content via the native Messages app or share sheet. Supports both iOS and Android with attachments (images, videos, PDFs).
Features
- 📱 Direct SMS - Open the Messages app directly with prefilled text and recipients
- 🔗 Share Sheet - Use the native share sheet to let users choose their preferred app
- 📎 Attachments - Send images, videos, PDFs, and other files as MMS
- 🤖 Android & iOS - Full support for both platforms
- ⚡ Turbo Module - Built with the new React Native architecture for better performance
Installation
npm install @sourceship13/react-native-share-sms
# or
yarn add @sourceship13/react-native-share-smsiOS Setup
Install CocoaPods dependencies:
cd ios && pod install && cd ..Android Setup
No additional setup required. The library automatically configures the necessary permissions.
Usage
Basic SMS
Send a simple text message:
import { sendSMS } from '@sourceship13/react-native-share-sms';
const sendMessage = async () => {
try {
const result = await sendSMS({
body: 'Hello from React Native!',
recipients: ['+1234567890'], // Optional: prefill recipient numbers
});
if (result.success) {
console.log('Message sent!');
} else {
console.log('Message cancelled or failed:', result.status);
}
} catch (error) {
console.error('Error:', error);
}
};Using Share Sheet
Let users choose which app to share with:
import { sendWithShareSMS } from '@sourceship13/react-native-share-sms';
const sendMessageWThruShare = async () => {
try {
const result = await sendWithShareSMS({
body: 'Check out this awesome content!',
});
console.log('Share result:', result.status);
} catch (error) {
console.error('Error:', error);
}
};With Attachments
Send images, videos, or other files:
import { sendSMS } from '@sourceship13/react-native-share-sms';
const sendWithAttachment = async () => {
try {
const result = await sendSMS({
body: 'Check out this photo!',
recipients: ['+1234567890'],
attachments: [
{
uri: 'file:///path/to/image.jpg',
mimeType: 'image/jpeg',
filename: 'photo.jpg', // Optional
},
],
});
console.log('Result:', result);
} catch (error) {
console.error('Error:', error);
}
};With Remote Images
The library supports remote URLs (http/https) - it will download and attach them automatically:
import { sendSMS } from '@sourceship13/react-native-share-sms';
const sendWithRemoteImage = async () => {
const result = await sendSMS({
body: 'Look at this!',
attachments: [
{
uri: 'https://example.com/image.png',
mimeType: 'image/png',
},
],
});
};API Reference
sendSMS(options: SMSOptions): Promise<SMSResult>
Opens the native Messages app directly with prefilled content.
sendWithShareSMS(options: SMSOptions): Promise<SMSResult>
Opens the native share sheet, allowing users to choose Messages or other apps.
sendSmsCustomShare(options: SMSOptions): Promise<SMSResult>
Shows a custom iOS-style share sheet displaying only messaging apps. This provides a cleaner, more focused sharing experience when you specifically want users to share via messaging.
import { sendSmsCustomShare } from '@sourceship13/react-native-share-sms';
const shareViaMessages = async () => {
const result = await sendSmsCustomShare({
body: 'Shared via custom messaging sheet!',
recipients: [],
});
if (result.success) {
console.log('Shared successfully!');
}
};Types
interface SMSOptions {
body: string; // The message text
recipients?: string[]; // Phone numbers to send to (optional)
attachments?: Attachment[]; // Files to attach (optional)
}
interface Attachment {
uri: string; // File URI (file://, http://, https://, or content://)
mimeType: string; // MIME type (e.g., 'image/jpeg', 'video/mp4')
filename?: string; // Optional filename
}
interface SMSResult {
success: boolean; // Whether the message was sent
status: 'sent' | 'cancelled' | 'failed' | 'unknown';
error?: string; // Error message if failed
}Supported MIME Types
| Type | Extensions |
|------|------------|
| Images | image/jpeg, image/png, image/gif, image/heic, image/webp |
| Videos | video/mp4, video/quicktime |
| Documents | application/pdf |
Example
See the example app for a complete working demo.
import React from 'react';
import { View, Button, Alert } from 'react-native';
import { sendSMS, sendWithShareSMS, sendSmsCustomShare } from '@sourceship13/react-native-share-sms';
export default function App() {
const handleSendSMS = async () => {
const result = await sendSMS({
body: 'Hello!',
recipients: [],
});
Alert.alert('Result', result.status);
};
const handleShare = async () => {
const result = await sendWithShareSMS({
body: 'Shared via share sheet!',
});
Alert.alert('Result', result.status);
};
const handleCustomShare = async () => {
const result = await sendSmsCustomShare({
body: 'Shared via custom messaging sheet!',
});
Alert.alert('Result', result.status);
};
return (
<View style={{ flex: 1, justifyContent: 'center', padding: 20, gap: 10 }}>
<Button title="Send SMS" onPress={handleSendSMS} />
<Button title="Share" onPress={handleShare} />
<Button title="Custom Share (Messages Only)" onPress={handleCustomShare} />
</View>
);
}Platform Notes
iOS
- Uses
MFMessageComposeViewControllerfor direct SMS - Uses
UIActivityViewControllerfor share sheet - Supports iMessage and SMS
Android
- Uses the default SMS app via
Intent.ACTION_SENDTO - Uses
Intent.ACTION_SENDfor share sheet - Automatically handles MMS for messages with attachments
Contributing
License
MIT
Made with create-react-native-library
