# receive_sharing_intent **Repository Path**: xia1999/receive_sharing_intent ## Basic Information - **Project Name**: receive_sharing_intent - **Description**: A Flutter plugin that enables flutter apps to receive sharing photos, text and url from other apps. - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-06-17 - **Last Updated**: 2024-06-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # receive_sharing_intent [![pub package](https://img.shields.io/pub/v/receive_sharing_intent.svg)](https://pub.dev/packages/receive_sharing_intent) A Flutter plugin that enables flutter apps to receive sharing photos, videos, text, urls or any other file types from another app. Also, supports iOS Share extension and launching the host app automatically. Check the provided [example](./example/lib/main.dart) for more info. | | Android | iOS | |-------------|-------------------------|-------------------| | **Support** | SDK 19+ (Kotlin 1.9.22) | 12.0+ (Swift 5.0) | ![Alt Text](./example/demo.gif) # Usage To use this plugin, add `receive_sharing_intent` as a [dependency in your pubspec.yaml file](https://flutter.io/platform-plugins/). For example: ```yaml dependencies: receive_sharing_intent: ^latest ``` ## Android Add the following filters to your [android/app/src/main/AndroidManifest.xml](./example/android/app/src/main/AndroidManifest.xml): ```xml .... ``` ## iOS #### 1. Create Share Extension - Using Xcode, go to File/New/Target and Choose "Share Extension". - Give it a name, i.e., "Share Extension". Make sure the deployment target for Runner.app and the share extension is the same. #### 2. Replace your [ios/Share Extension/Info.plist](./example/ios/Share%20Extension/Info.plist) with the following: ```xml AppGroupId $(CUSTOM_GROUP_ID) CFBundleVersion $(FLUTTER_BUILD_NUMBER) NSExtension NSExtensionAttributes PHSupportedMediaTypes Video Image NSExtensionActivationRule NSExtensionActivationSupportsText NSExtensionActivationSupportsWebURLWithMaxCount 1 NSExtensionActivationSupportsImageWithMaxCount 100 NSExtensionActivationSupportsMovieWithMaxCount 100 NSExtensionActivationSupportsFileWithMaxCount 1 NSExtensionMainStoryboard MainInterface NSExtensionPointIdentifier com.apple.share-services ``` #### 3. Add the following to your [ios/Runner/Info.plist](./example/ios/Runner/Info.plist): ```xml ... AppGroupId $(CUSTOM_GROUP_ID) CFBundleURLTypes CFBundleTypeRole Editor CFBundleURLSchemes ShareMedia-$(PRODUCT_BUNDLE_IDENTIFIER) NSPhotoLibraryUsageDescription To upload photos, please allow permission to access your photo library. ... ``` #### 4. Add the following to your [ios/Runner/Runner.entitlements](./example/ios/Runner/Runner.entitlements): ```xml .... com.apple.developer.associated-domains applinks:example.com .... ``` #### 5. Add the following to your [ios/Podfile](./example/ios/Podfile): ```ruby ... target 'Runner' do use_frameworks! use_modular_headers! flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__)) # Share Extension is name of Extension which you created which is in this case 'Share Extension' target 'Share Extension' do inherit! :search_paths end end ... ``` #### 6. Add Runner and Share Extension in the same group * Go to `Signing & Capabilities` tab and add App Groups capability in **BOTH** Targets: `Runner` and `Share Extension` * Add a new container with the name of your choice. For example `group.MyContainer` in the example project its `group.com.kasem.ShareExtention` * Add User-defined(`Build Settings -> +`) string `CUSTOM_GROUP_ID` in **BOTH** Targets: `Runner` and `Share Extension` and set value to group id created above. You can use different group ids depends on your flavor schemes #### 7. Go to Build Phases of your Runner target and move `Embed Foundation Extension` to the top of `Thin Binary`. #### 8. Make your `ShareViewController` [ios/Share Extension/ShareViewController.swift](./example/ios/Share%20Extension/ShareViewController.swift) inherit from `RSIShareViewController`: ```swift // If you get no such module 'receive_sharing_intent' error. // Go to Build Phases of your Runner target and // move `Embed Foundation Extension` to the top of `Thin Binary`. import receive_sharing_intent class ShareViewController: RSIShareViewController { // Use this method to return false if you don't want to redirect to host app automatically. // Default is true override func shouldAutoRedirect() -> Bool { return false } } ``` #### Compiling issues and their fixes * Error: No such module 'receive_sharing_intent' * Fix: Go to Build Phases of your Runner target and move `Embed Foundation Extension` to the top of `Thin Binary`. * Error: App does not build after adding Share Extension? * Fix: Check Build Settings of your share extension and remove everything that tries to import Cocoapods from your main project. i.e. remove everything under `Linking/Other Linker Flags` * You might need to disable bitcode for the extension target * Error: Invalid Bundle. The bundle at 'Runner.app/Plugins/Sharing Extension.appex' contains disallowed file 'Frameworks' * Fix: https://stackoverflow.com/a/25789145/2061365 ## Full Example [main.dart](./example/lib/main.dart) ```dart import 'package:flutter/material.dart'; import 'dart:async'; import 'package:receive_sharing_intent/receive_sharing_intent.dart'; void main() => runApp(MyApp()); class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State { late StreamSubscription _intentSub; final _sharedFiles = []; @override void initState() { super.initState(); // Listen to media sharing coming from outside the app while the app is in the memory. _intentSub = ReceiveSharingIntent.instance.getMediaStream().listen((value) { setState(() { _sharedFiles.clear(); _sharedFiles.addAll(value); print(_sharedFiles.map((f) => f.toMap())); }); }, onError: (err) { print("getIntentDataStream error: $err"); }); // Get the media sharing coming from outside the app while the app is closed. ReceiveSharingIntent.instance.getInitialMedia().then((value) { setState(() { _sharedFiles.clear(); _sharedFiles.addAll(value); print(_sharedFiles.map((f) => f.toMap())); // Tell the library that we are done processing the intent. ReceiveSharingIntent.instance.reset(); }); }); } @override void dispose() { _intentSub.cancel(); super.dispose(); } @override Widget build(BuildContext context) { const textStyleBold = const TextStyle(fontWeight: FontWeight.bold); return MaterialApp( home: Scaffold( appBar: AppBar( title: const Text('Plugin example app'), ), body: Center( child: Column( children: [ Text("Shared files:", style: textStyleBold), Text(_sharedFiles .map((f) => f.toMap()) .join(",\n****************\n")), ], ), ), ), ); } } ```