Native Social Share Plugin

Overview

The Native Social Share plugin allows Flutter developers to easily share files (images, text, etc.) to popular social media platforms such as Facebook, Twitter, WhatsApp, and more. This plugin provides native functionality to share content directly from your app.

Setup Instructions

Step 1: Add file_paths.xml

For the plugin to properly share files, you need to configure file access via a FileProvider. This requires adding a file_paths.xml file to your Android project.

  1. Create a new XML file in your project under the path android/app/src/main/res/xml/ and name it file_paths.xml.
  2. Copy the following content into the file_paths.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <paths>
        <root-path
            name="root"
            path="." />
        <external-path
            name="external"
            path="." />
        <external-files-path
            name="external_files"
            path="." />
        <cache-path
            name="cache"
            path="." />
        <external-cache-path
            name="external_cache"
            path="." />
        <files-path
            name="files"
            path="." />
    </paths>
</resources>

This configuration allows the FileProvider to grant URI access to various paths, such as external storage, cache, and app files.

Step 2: Update AndroidManifest.xml

Next, you need to configure the FileProvider in your AndroidManifest.xml file to work with the paths you've just defined.

  1. Open the AndroidManifest.xml file located at android/app/src/main/AndroidManifest.xml.
  2. Add the following provider entry inside the <application> tag:
<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

This tells Android that the FileProvider should use the paths defined in file_paths.xml to share files. The android:authorities attribute must match the application ID defined in your build.gradle file, which is usually ${applicationId}.

Step 3: Flutter Plugin Installation

  1. Add the plugin to your pubspec.yaml file:
dependencies:
  native_social_share: ^0.0.4
  1. Run the following command to install the plugin:
flutter pub get

Step 4: Usage Example

After setting up the plugin, you can start using it to share content. Here is an example of how to use the plugin:

import 'package:native_social_share/native_social_share.dart';

// Share content
NativeSocialShare.shareContent("Share this text", "image.jpg");

Step 5: Additional Configuration

Ensure that the necessary permissions are added to your AndroidManifest.xml for accessing storage and sharing files. This is particularly important for Android versions before Android 10.

Example permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />

Troubleshooting

  • Error: FileProvider setup issues Ensure that your file_paths.xml is correctly placed in android/app/src/main/res/xml/ and that the authority in the manifest matches the application ID.

  • Permissions not granted If you're testing on Android 10 or above, ensure you have requested the appropriate runtime permissions using the permission_handler package or similar methods.


For iOS

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <!-- ... other keys ... -->

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>whatsapp</string>
        <string>whatsapp-business</string> // For WhatsApp Business
        <string>instagram</string>
        <string>instagram-stories</string> // For "com.instagram.exclusivegram" / stories
        <string>fb</string>             // Facebook general scheme
        <string>fbapi</string>
        <string>fbauth2</string>
        <string>fbshareextension</string> // For Facebook sharing
    </array>

    <!-- If you ever need to save images to the photo library first (not current approach) -->
    <!-- <key>NSPhotoLibraryUsageDescription</key> -->
    <!-- <string>This app needs access to your photo library to share images.</string> -->
    <!-- <key>NSPhotoLibraryAddUsageDescription</key> -->
    <!-- <string>This app needs permission to save images to your photo library for sharing.</string> -->

    <!-- ... other keys ... -->
</dict>
</plist>

License

This plugin is released under the MIT License. See the LICENSE file for more details.