mallem mahesh
mallem mahesh

Reputation: 11

in xamarin forms how to share images and text with facebook and g+ and twitter

I am working for Xamarin-Forms app where I have to share images and text with social networks. I have tried with Xamarin.auth plugin, but it is not working for me. Please suggest any other plugins for social sharing.

Upvotes: 1

Views: 3230

Answers (2)

Dean Thomas
Dean Thomas

Reputation: 41

For those who still looking, I added a few fixes to Venkata Swamy's code, at Xamarin.Android, it's not working anymore for deprecated function etc.

    public async void Share(string message, ImageSource image)
    {
        var intent = new Intent(Intent.ActionSend);
        intent.PutExtra(Intent.ExtraText, message);
        intent.SetType("image/png");

        IImageSourceHandler handler = null;
        if (image is UriImageSource)
            handler = new ImageLoaderSourceHandler();
        else if (image is FileImageSource)
            handler = new FileImageSourceHandler();
        else if (image is StreamImageSource) handler = new StreamImagesourceHandler();
        var bitmap = await handler.LoadImageAsync(image, Application.Context);

        var path = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads + File.Separator + "logo.png");

        using (var os = new FileStream(path.AbsolutePath, FileMode.Create))
        {
            bitmap.Compress(Bitmap.CompressFormat.Png, 100, os);
        }

        var imageUri = FileProvider.GetUriForFile(Application.Context, "com.uajy.atmarewards.fileprovider", path);
        intent.PutExtra(Intent.ExtraStream, imageUri);
        var newIntent = Intent.CreateChooser(intent, "Share Image");
        newIntent.SetFlags(ActivityFlags.NewTask);
        Application.Context.StartActivity(newIntent);
    }

I didn't use the subject parameter, so I deleted it. And the text somehow didn't show, I haven't found the fix for that. For anyone who found the way to show the message text, please give an answer and comment so I could reach your fixes.

Upvotes: 0

Venkata Swamy Balaraju
Venkata Swamy Balaraju

Reputation: 1481

In PCL:

using System;
using Xamarin.Forms;

namespace ShareSample
{
  public interface IShare
  {
     void Share(string subject, string message, ImageSource image);
  }
}

Xamarin.Android:

using Android.App;
using Android.Content;
using Android.Graphics;
using Android.OS;
using ShareSample.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: Dependency(typeof(IShareService))]
namespace ShareSample.Droid
{
  public class IShareService : Activity, IShare
  {
     public async void Share(string subject, string message, 
     ImageSource image)
    {
        var intent = new Intent(Intent.ActionSend);
        //intent.PutExtra(Intent.ExtraSubject, subject);
        intent.PutExtra(Intent.ExtraText, message);
        intent.SetType("image/png");

        var handler = new ImageLoaderSourceHandler();
        var bitmap = await handler.LoadImageAsync(image, this);

        var path = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads
            + Java.IO.File.Separator + "logo.png");

        using (var os = new System.IO.FileStream(path.AbsolutePath, System.IO.FileMode.Create))
        {
            bitmap.Compress(Bitmap.CompressFormat.Png, 100, os);
        }

        intent.PutExtra(Intent.ExtraStream, Android.Net.Uri.FromFile(path));
        Forms.Context.StartActivity(Intent.CreateChooser(intent, "Share Image"));
    }
}

}

Xamarin.iOS:

 using Foundation;
 using ShareSample.iOS;
 using UIKit;
 using Xamarin.Forms;
 using Xamarin.Forms.Platform.iOS;

 [assembly: Dependency(typeof(IShareService))]
 namespace ShareSample.iOS
 {
  public class IShareService : IShare
  {
    public async void Share(string subject, string message, ImageSource image)
    {
        var handler = new ImageLoaderSourceHandler();
        var uiImage = await handler.LoadImageAsync(image);

        var img = NSObject.FromObject(uiImage);
        var mess = NSObject.FromObject(message);

        var activityItems = new[] { mess, img };
        var activityController = new UIActivityViewController(activityItems, null);

        var topController = UIApplication.SharedApplication.KeyWindow.RootViewController;

        while (topController.PresentedViewController != null)
        {
            topController = topController.PresentedViewController;
        }

        topController.PresentViewController(activityController, true, () => { });
    }

  }

}

In PCL call DependencyService:

  using System;
  using Xamarin.Forms;

 namespace ShareSample
 {
    public class SharePage : ContentPage
    {
      public SharePage()
      {
        Button sharebutton = new Button()
        {
            Text = "Share",
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.CenterAndExpand,
            TextColor = Color.White,
            BackgroundColor = Color.Blue

        };

        Image img = new Image()
        {
            Source = "http://www.wintellect.com/devcenter/wp-content/uploads/2013/10/Wintellect_logo.gif",
            Aspect = Aspect.AspectFit
        };

        sharebutton.Clicked += (sender, e) =>
        {
            DependencyService.Get<IShare>().Share(" ", "Hi Balaraju. How are you?", img.Source);
        };

        StackLayout stack = new StackLayout()
        {
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand,
            BackgroundColor = Color.Aqua,
            Children = { sharebutton }
        };

        Content = stack;
        Padding = new Thickness(0, 20, 0, 0);
    }
  }
 }

No need any plugin, We can also share Text with image also,

please find the DropBox link for sample:

https://www.dropbox.com/s/32o9uuew369yupi/ShareSample.zip?dl=0

Here the problem is if you want share an image or text the perticular app available in you device like facebook, twitter

(OR)

Without application in your device:

Use Xamarin.Auth

http://www.c-sharpcorner.com/article/oauth-login-authenticating-with-identity-provider-in-xamarin-forms/

http://www.c-sharpcorner.com/article/register-identity-provider-for-new-oauth-application/

https://visualstudiomagazine.com/articles/2014/04/01/using-oauth-twitter-and-async-to-display-data.aspx?m=2

https://github.com/HoussemDellai/Facebook-Login-Xamarin-Forms

Upvotes: 7

Related Questions