Skip to content

Instantly share code, notes, and snippets.

@brunoportess
Created May 14, 2018 11:26
Show Gist options
  • Save brunoportess/b57aa08a00e031a3f8bf3490842a5714 to your computer and use it in GitHub Desktop.
Save brunoportess/b57aa08a00e031a3f8bf3490842a5714 to your computer and use it in GitHub Desktop.
Implementação de SHARE no xamarin forms
//interface do projeto core
public interface IShare
{
Task Show(string title, string message, string filePath);
}
//Classe do projeto android
[assembly: Xamarin.Forms.Dependency(typeof(Share))]
namespace FindenApp.Droid.Helpers
{
public class Share : FindenApp.Helpers.IShare
{
private readonly Context _context;
public Share()
{
//_context = Android.App.Application.Context;
_context = Application.Context;
}
public Task Show(string title, string message, string filePath)
{
var extension = filePath.Substring(filePath.LastIndexOf(".") + 1).ToLower();
var contentType = string.Empty;
// verifica a extensão do arquivo
switch (extension)
{
case "pdf":
contentType = "application/pdf";
break;
case "png":
contentType = "image/png";
break;
default:
contentType = "text/plain";
break;
}
var intent = new Intent(Intent.ActionSend);
intent.SetType(contentType);
//FILE
//intent.PutExtra(Intent.ExtraStream, Uri.Parse("file://" + filePath));
//URL
intent.AddFlags(ActivityFlags.ClearWhenTaskReset);
//intent.PutExtra(Intent.ExtraText, string.Empty);
intent.PutExtra(Intent.ExtraText, filePath);
intent.PutExtra(Intent.ExtraSubject, message);
var chooserIntent = Intent.CreateChooser(intent, title ?? string.Empty);
chooserIntent.SetFlags(ActivityFlags.ClearTop);
chooserIntent.SetFlags(ActivityFlags.NewTask);
_context.StartActivity(chooserIntent);
return Task.FromResult(true);
}
}
}
//Classe do projeto iOs
[assembly: Dependency(typeof(Share))]
namespace FindenApp.iOS.Helpers
{
public class Share : FindenApp.Helpers.IShare
{
// MUST BE CALLED FROM THE UI THREAD
public async Task Show(string title, string message, string filePath)
{
var items = new NSObject[] { NSObject.FromObject(title), NSUrl.FromFilename(filePath) };
var activityController = new UIActivityViewController(items, null);
var vc = GetVisibleViewController();
NSString[] excludedActivityTypes = null;
if (excludedActivityTypes != null && excludedActivityTypes.Length > 0)
activityController.ExcludedActivityTypes = excludedActivityTypes;
if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
if (activityController.PopoverPresentationController != null)
{
activityController.PopoverPresentationController.SourceView = vc.View;
}
}
await vc.PresentViewControllerAsync(activityController, true);
}
UIViewController GetVisibleViewController()
{
var rootController = UIApplication.SharedApplication.KeyWindow.RootViewController;
if (rootController.PresentedViewController == null)
return rootController;
if (rootController.PresentedViewController is UINavigationController)
{
return ((UINavigationController)rootController.PresentedViewController).TopViewController;
}
if (rootController.PresentedViewController is UITabBarController)
{
return ((UITabBarController)rootController.PresentedViewController).SelectedViewController;
}
return rootController.PresentedViewController;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment