Social share with Xamarin.Forms

Hi Guys,

Today, I am going to teach you about Social sharing with Xamarin.Forms.

Technically, there is no inbuilt class or method to perform a social share  in Xamarin.Forms.

So, How we are going to do this.

Dependancy services ?

Yes… 🙂 that’s the only way…

Lets create.

  1. Create a Xamarin.Forms project.

Screen Shot 2017-03-08 at 12.04.16 PM

 

2. Create a HomePage and set it as the MainPage in App.xaml.

Screen Shot 2017-03-08 at 12.08.24 PM

 

3. Create following classes.

  • IShareService.cs in PLC project
  • ShareService.cs in .Droid project
  • ShareService.cs in .iOS project

Screen Shot 2017-03-08 at 12.22.12 PM

IShareService.cs

namespace SocialApp
{
    public interface IShareService
    {
        void SharePageLink(string link);
    }
}

 

ShareService.cs   in .Droid

using Android.App;
using Android.Content;
using Xamarin.Forms;
using SocialApp.Droid;

[assembly: Dependency(typeof(ShareService))]
namespace SocialApp.Droid
{
    public class ShareService : IShareService
    {
        public void SharePageLink(string url)
        {
            var context = Forms.Context;
            Activity activity = context as Activity;

            Intent share = new Intent(Intent.ActionSend);
            share.SetType(text/plain);
            share.AddFlags(ActivityFlags.ClearWhenTaskReset);
            share.PutExtra(Intent.ExtraSubject, Brusselslife);
            share.PutExtra(Intent.ExtraText, url);

            activity.StartActivity(Intent.CreateChooser(share, Share link!));
        }
    }
}

 

ShareService.cs   in .iOS

using Foundation;
using UIKit;
using Xamarin.Forms;
using SocialApp.iOS;

[assembly: Dependency(typeof(ShareService))]
namespace SocialApp.iOS
{
    public class ShareService : IShareService
    {
        public void SharePageLink(string url)
        {
            var window = UIApplication.SharedApplication.KeyWindow;
            var rootViewController = window.RootViewController;

            var activityViewController = new UIActivityViewController(new NSString[] { new NSString(url) }, null);
            activityViewController.ExcludedActivityTypes = new NSString[] {
                UIActivityType.AirDrop,
                UIActivityType.Print,
                UIActivityType.Message,
                UIActivityType.AssignToContact,
                UIActivityType.SaveToCameraRoll,
                UIActivityType.AddToReadingList,
                UIActivityType.PostToFlickr,
                UIActivityType.PostToVimeo,
                UIActivityType.PostToTencentWeibo,
                UIActivityType.PostToWeibo
            };

            rootViewController.PresentViewController(activityViewController, true, null);
        }
    }
}

 

Save all.

Now our SocialShare service is ready for use.

 

3.  Add a button with event handler in HomePage.

<?xml version=1.0 encoding=UTF8?>
<ContentPage xmlns=http://xamarin.com/schemas/2014/forms xmlns:x=http://schemas.microsoft.com/winfx/2009/xaml x:Class=SocialApp.HomePage>
    <ContentPage.Content>
        <Button Clicked=OnShare HorizontalOptions=CenterAndExpand VerticalOptions=Center WidthRequest=200 Text=Share TextColor=White BackgroundColor=Purple/>
    </ContentPage.Content>
</ContentPage>

 

using System;
using Xamarin.Forms;

namespace SocialApp
{
    public partial class HomePage : ContentPage
    {
        public HomePage()
        {
            InitializeComponent();
        }

        private void OnShare(object sender, EventArgs e)
        {
            string shareLink = http://hiranpeiris.com;
            DependencyService.Get<IShareService>().SharePageLink(shareLink);
        }
    }
}

 

You can share,

Link, Youtube Video, Picture url or Text.

 

4. Compile and run on Android.

Screenshot_1488956879          The+Hindu+Android+App-Makes+sharing+easy-2.png

 

5. Compile and run on iOS.

Screen Shot 2017-03-08 at 12.49.57 PM       tQtSC

 

Done… 🙂

Source code available in https://github.com/hiranpeiris/SocialApp

Happy coding… 🙂

2 thoughts on “Social share with Xamarin.Forms

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s