Skip to content

Commit

Permalink
be able to share video to instagram
Browse files Browse the repository at this point in the history
  • Loading branch information
LeGoffMael authored and Mael Le Goff committed Jan 4, 2022
1 parent 857fbd4 commit acc7858
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 27 deletions.
26 changes: 16 additions & 10 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,23 @@ class _MyAppState extends State<MyApp> {
Future<void> pickImage() async {
final XFile? xFile = await picker.pickImage(source: ImageSource.gallery);
print(xFile);
file = File(xFile!.path);
setState(() {
videoEnable = false;
});
if (xFile != null) {
file = File(xFile.path);
setState(() {
videoEnable = false;
});
}
}

Future<void> pickVideo() async {
final XFile? xFile = await picker.pickVideo(source: ImageSource.camera);
final XFile? xFile = await picker.pickVideo(source: ImageSource.gallery);
print(xFile);
file = File(xFile!.path);
setState(() {
videoEnable = true;
});
if (xFile != null) {
file = File(xFile.path);
setState(() {
videoEnable = true;
});
}
}

Future<void> onButtonTap(Share share) async {
Expand Down Expand Up @@ -132,7 +136,9 @@ class _MyAppState extends State<MyApp> {
message: msg, phoneNumber: 'phone-number-with-country-code');
break;
case Share.share_instagram:
response = await flutterShareMe.shareToInstagram(imagePath: file!.path);
response = await flutterShareMe.shareToInstagram(
filePath: file!.path,
fileType: videoEnable ? FileType.video : FileType.image);
break;
case Share.share_telegram:
response = await flutterShareMe.shareToTelegram(msg: msg);
Expand Down
32 changes: 27 additions & 5 deletions ios/Classes/SwiftFlutterShareMePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,23 @@ public class SwiftFlutterShareMePlugin: NSObject, FlutterPlugin, SharingDelegate
// share image via instagram stories.
// @ args image url
func shareInstagram(args:Dictionary<String,Any>) {
let imageUrl=args["url"] as! String
let fileUrl=args["url"] as! String
let type=args["fileType"] as! String

let image = UIImage(named: imageUrl)
if(image==nil){
var image:UIImage?
var video:URL?

if(type == "image") {
image = UIImage(named: fileUrl)
} else if(type == "video") {
video = URL(fileURLWithPath: fileUrl)
}

if(image==nil && video==nil){
self.result!("File format not supported Please check the file.")
return;
}

guard let instagramURL = NSURL(string: "instagram://app") else {
if let result = result {
self.result?("Instagram app is not installed on your device")
Expand All @@ -290,8 +300,20 @@ public class SwiftFlutterShareMePlugin: NSObject, FlutterPlugin, SharingDelegate

do{
try PHPhotoLibrary.shared().performChangesAndWait {
let request = PHAssetChangeRequest.creationRequestForAsset(from: image!)
let assetId = request.placeholderForCreatedAsset?.localIdentifier
var request:PHAssetChangeRequest?

if(image != nil) {
request = PHAssetChangeRequest.creationRequestForAsset(from: image!)
} else if (video != nil) {
request = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: video!)
}

if(request==nil){
self.result!("An error occured while saving the file.")
return;
}

let assetId = request!.placeholderForCreatedAsset?.localIdentifier
let instShareUrl:String? = "instagram://library?LocalIdentifier=" + assetId!

//Share image
Expand Down
39 changes: 27 additions & 12 deletions lib/flutter_share_me.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class FlutterShareMe {
/// For ios
/// If include image then text params will be ingored as there is no current way in IOS share both at the same.
Future<String?> shareToWhatsApp(
{String msg = '', String imagePath = '', FileType? fileType = FileType.image}) async {
{String msg = '',
String imagePath = '',
FileType? fileType = FileType.image}) async {
final Map<String, dynamic> arguments = <String, dynamic>{};
arguments.putIfAbsent('msg', () => msg);
arguments.putIfAbsent('url', () => imagePath);
Expand Down Expand Up @@ -54,22 +56,24 @@ class FlutterShareMe {

String? result;
try {
result = await _channel.invokeMethod<String>(_methodWhatsAppPersonal, arguments);
result = await _channel.invokeMethod<String>(
_methodWhatsAppPersonal, arguments);
} catch (e) {
return e.toString();
}

return result;
}

///share to Telegram
/// [msg] message text you want on telegram
Future<String?> shareToTelegram(
{required String msg}) async {
Future<String?> shareToTelegram({required String msg}) async {
final Map<String, dynamic> arguments = <String, dynamic>{};
arguments.putIfAbsent('msg', () => msg);
String? result;
try {
result = await _channel.invokeMethod<String>(_methodTelegramShare, arguments);
result =
await _channel.invokeMethod<String>(_methodTelegramShare, arguments);
} catch (e) {
return e.toString();
}
Expand All @@ -80,14 +84,16 @@ class FlutterShareMe {
///[imagePath] is local image
/// For ios
/// If include image then text params will be ingored as there is no current way in IOS share both at the same.
Future<String?> shareToWhatsApp4Biz({String msg = '', String? imagePath = ''}) async {
Future<String?> shareToWhatsApp4Biz(
{String msg = '', String? imagePath = ''}) async {
final Map<String, dynamic> arguments = <String, dynamic>{};

arguments.putIfAbsent('msg', () => msg);
arguments.putIfAbsent('url', () => imagePath);
String? result;
try {
result = await _channel.invokeMethod<String>(_methodWhatsAppBusiness, arguments);
result = await _channel.invokeMethod<String>(
_methodWhatsAppBusiness, arguments);
} catch (e) {
return 'false';
}
Expand All @@ -96,7 +102,8 @@ class FlutterShareMe {
}

///share to facebook
Future<String?> shareToFacebook({required String msg, String url = ''}) async {
Future<String?> shareToFacebook(
{required String msg, String url = ''}) async {
final Map<String, dynamic> arguments = <String, dynamic>{};
arguments.putIfAbsent('msg', () => msg);
arguments.putIfAbsent('url', () => url);
Expand Down Expand Up @@ -128,21 +135,29 @@ class FlutterShareMe {
Future<String?> shareToSystem({required String msg}) async {
String? result;
try {
result = await _channel.invokeMethod<String>(_methodSystemShare, {'msg': msg});
result =
await _channel.invokeMethod<String>(_methodSystemShare, {'msg': msg});
} catch (e) {
return 'false';
}
return result;
}

///share file to instagram
Future<String?> shareToInstagram({required String imagePath}) async {
Future<String?> shareToInstagram(
{required String filePath, FileType fileType = FileType.image}) async {
final Map<String, dynamic> arguments = <String, dynamic>{};
arguments.putIfAbsent('url', () => imagePath);
arguments.putIfAbsent('url', () => filePath);
if (fileType == FileType.image) {
arguments.putIfAbsent('fileType', () => 'image');
} else {
arguments.putIfAbsent('fileType', () => 'video');
}
String? result;

try {
result = await _channel.invokeMethod<String>(_methodInstagramShare, arguments);
result =
await _channel.invokeMethod<String>(_methodInstagramShare, arguments);
} catch (e) {
return e.toString();
}
Expand Down

0 comments on commit acc7858

Please sign in to comment.