Skip to content

Latest commit

 

History

History
278 lines (242 loc) · 8.86 KB

README.md

File metadata and controls

278 lines (242 loc) · 8.86 KB

md_insta_fb_share

A new Flutter plugin was created while using the framework to develop cross-platform apps at MobiDev

Getting Started

This project is a starting point for a Flutter plug-in package, a specialized package that includes platform-specific implementation code for Android and/or iOS.

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

MdInstaFbShare

A Flutter plugin that allows to share images to instagram or facebook

Example

To use this plugin :

  dependencies:
    flutter:
      sdk: flutter
    md_insta_fb_share:

Example

Share methods return ShareStatus

ShareStatus {
  success, // asset successfully sent to the share app (share app may not have persmissions which may throw an error inside share app)
  appCanNotBeOpenedError,  // share app can not be opened for some reason (app will be opened on store)
  imageNotFoundError, // can not find asset
  galleryAccessError, // can not save image in image gallery (requires for insta feed share)
  unknownError // unknown error
}
import 'dart:io';
import 'package:path/path.dart' as p;
import 'package:flutter/material.dart';
import 'package:md_insta_fb_share/md_insta_fb_share.dart';
import 'package:path_provider/path_provider.dart';
import 'package:flutter/services.dart' show rootBundle;

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              ElevatedButton(
                onPressed: () async {
                  final dir = (await getTemporaryDirectory()).path;

                  final data = await rootBundle.load('assets/insta_big.png');
                  final buffer = data.buffer;
                  final fileName = 'insta_big-${DateTime.now().millisecondsSinceEpoch}.png';
                  final file = await File('$dir/$fileName').writeAsBytes(
                      buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));

                  MdInstaFbShare.shareInstaStory(file.path);
                },
                child: const Text('Test insta story share')
              ),
              ElevatedButton(
                  onPressed: () async {
                    final dir = (await getTemporaryDirectory()).path;

                    final data = await rootBundle.load('assets/insta_big.png');
                    final buffer = data.buffer;
                    final fileName =  'insta_big-${DateTime.now().millisecondsSinceEpoch}.png';
                    final file = await File('$dir/$fileName').writeAsBytes(
                        buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));

                    MdInstaFbShare.shareInstaFeed(file.path);
                  },
                  child: const Text('Test insta feed share')
              ),
              ElevatedButton(
                  onPressed: () async {
                    final dir = (await getTemporaryDirectory()).path;

                    final data = await rootBundle.load('assets/insta_big.png');
                    final buffer = data.buffer;
                    final fileName = 'insta_big-${DateTime.now().millisecondsSinceEpoch}.png';
                    final file = await File('$dir/$fileName').writeAsBytes(
                        buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
                    MdInstaFbShare.shareFBStory(file.path);
                  },
                  child: const Text('Test FB story share')
              ),
              ElevatedButton(
                  onPressed: () async {
                    final dir = (await getTemporaryDirectory()).path;

                    final data = await rootBundle.load('assets/insta_big.png');
                    final buffer = data.buffer;
                    final fileName = 'insta_big-${DateTime.now().millisecondsSinceEpoch}.png';
                    final file = await File('$dir/$fileName').writeAsBytes(
                        buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
                    MdInstaFbShare.shareFBFeed(file.path);
                  },
                  child: const Text('Test FB feed share')
              ),
              Image.asset('assets/insta_big.png', width: 150, fit: BoxFit.fitWidth),
              Row(
                children: [
                  const Text('FB App available: '),
                  FutureBuilder(
                    future: MdInstaFbShare.checkFBInstalled(),
                    builder: (_, snapshot) {
                      if (snapshot.hasData) {
                        return Text('${snapshot.data}');
                      }
                      return Container();
                    },
                  )
                ],
              ),
              Row(
                children: [
                  const Text('Insta App available: '),
                  FutureBuilder(
                    future: MdInstaFbShare.checkInstaInstalled(),
                    builder: (_, snapshot) {
                      if (snapshot.hasData) {
                        return Text('${snapshot.data}');
                      }
                      return Container();
                    },
                  )
                ],
              )
            ],
          )
        ),
      ),
    );
  }
}

iOS

<!--Info.plist-->
<key>FacebookAppID</key>
<string>{Your-FB-APP-ID}</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
    <key>CFBundleURLSchemes</key>
    <array>
        <string>fb{Your-FB-APP-ID}</string>
    </array>
</dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram-stories</string>
<string>facebook-stories</string>
<string>facebook</string>
<string>instagram</string>
<string>fbauth2</string>
<string>fbauth2</string>
<string>fbapi</string>
<string>fbapi20130214</string>
<string>fbapi20130410</string>
<string>fbapi20130702</string>
<string>fbapi20131010</string>
<string>fbapi20131219</string>
<string>fbapi20140410</string>
<string>fbapi20140116</string>
<string>fbapi20150313</string>
<string>fbapi20150629</string>
<string>fbapi20160328</string>
<string>fbauth</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
<key>NSPhotoLibraryUsageDescription</key>
<string>Allow access to photo library</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Allow access to photo library</string>
// AppDelegate.swift
import UIKit
import Flutter
import FBSDKCoreKit <-- add this

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    ApplicationDelegate.shared.initializeSDK(); <-- add this
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

Android

<!--inside /android/src/main/AndroidManifest.xml-->
<application>
<!--your code-->
<provider android:authorities="com.facebook.app.FacebookContentProvider{Your-FB-APP-ID}"
            android:name="com.facebook.FacebookContentProvider"
            android:exported="true"/>

<meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id" />
<!--your code-->
</application>
<!--app/src/main/res/values/strings.xml-->
<string name="facebook_app_id">{Your FB app ID}</string>
<!--app/src/main/build.gradle-->
dependencies {
    implementation 'com.facebook.android:facebook-share:latest.release'
}

Sharing FaceBook feed

Sharing Facebook feed requires available internet connection, nothing will happen if not. You can use internet_connection_checker library. It checks exactly if internet access is available.

Example

  if(await InternetConnectionChecker().hasConnection) {
      MdInstaFbShare.shareFBFeed(file.path);
  }else{
      <!--your actions-->
  }

Getting Started

For help getting started with Flutter, view our online documentation.

For help on editing plugin code, view the documentation.