You're viewing an older version of this documentation. View the latest version (3.x)
Share
Overview#
The Share API enables users to share content from your app using the native share sheet. On iOS, this opens the native share menu with options like Messages, Mail, and social media apps. On Android, it launches the system share intent with available apps.
Import
Copied!
use Native\Mobile\Facades\Share;
Copied!
import { share } from '#nativephp';
Methods#
url()#
Share a URL using the native share dialog.
Parameters:
string $title- Title/subject for the sharestring $text- Text content or message to sharestring $url- URL to share
Returns: void
Share URL
Copied!
Share::url( title: 'Check this out', text: 'I found something interesting', url: 'https://example.com/article');
Copied!
await share.url( 'Check this out', 'I found something interesting', 'https://example.com/article');
file()#
Share a file using the native share dialog.
Parameters:
string $title- Title/subject for the sharestring $text- Text content or message to sharestring $filePath- Absolute path to the file to share
Returns: void
Share File
Copied!
Share::file( title: 'Share Document', text: 'Check out this PDF', filePath: '/path/to/document.pdf');
Copied!
await share.file( 'Share Document', 'Check out this PDF', '/path/to/document.pdf');
Examples#
Sharing a Website Link#
Share a link to your app's website or external content.
Share Website Link
Copied!
Share::url( title: 'My Awesome App', text: 'Download my app today!', url: 'https://myapp.com');
Copied!
await share.url( 'My Awesome App', 'Download my app today!', 'https://myapp.com');
Sharing Captured Photos#
Share a photo that was captured with the camera.
Share Captured Photo
Copied!
use Native\Mobile\Attributes\OnNative;use Native\Mobile\Events\Camera\PhotoTaken; #[OnNative(PhotoTaken::class)]public function handlePhotoTaken(string $path){ Share::file( title: 'My Photo', text: 'Check out this photo I just took!', filePath: $path );}
Copied!
import { share, on, off, Events } from '#nativephp';import { onMounted, onUnmounted } from 'vue'; const handlePhotoTaken = (payload) => { share.file( 'My Photo', 'Check out this photo I just took!', payload.path );}; onMounted(() => { on(Events.Camera.PhotoTaken, handlePhotoTaken);}); onUnmounted(() => { off(Events.Camera.PhotoTaken, handlePhotoTaken);});
Copied!
import { share, on, off, Events } from '#nativephp';import { useEffect } from 'react'; const handlePhotoTaken = (payload) => { share.file( 'My Photo', 'Check out this photo I just took!', payload.path );}; useEffect(() => { on(Events.Camera.PhotoTaken, handlePhotoTaken); return () => { off(Events.Camera.PhotoTaken, handlePhotoTaken); };}, []);
Notes#
- The native share sheet opens, allowing users to choose which app to share with (Messages, Email, social media, etc.)
- The file path must be absolute and the file must exist before calling the share method
- File paths should be verified to exist before attempting to share to avoid errors
- The Share API works with any file type (PDF, images, videos, documents, etc.)
- There is no way to determine which app the user selected or whether they cancelled the share
- No events are dispatched by the Share API
- The
url()method works with any URL format (http, https, deep links, etc.)
Plugin Dev Kit
Build native plugins with
Claude Code
Learn More
NativePHP Ultra
All NativePHP plugins, teams & priority support from
$35/mo
Learn More
The Vibes
The unofficial Laracon US
Day 3
Grab Your Spot
Only 100 tickets!
The Masterclass
Go from zero to
published app
in no time
in no time
Learn More
Early Bird Pricing