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


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.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[] {

            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= xmlns:x= x:Class=SocialApp.HomePage>
        <Button Clicked=OnShare HorizontalOptions=CenterAndExpand VerticalOptions=Center WidthRequest=200 Text=Share TextColor=White BackgroundColor=Purple/>


using System;
using Xamarin.Forms;

namespace SocialApp
    public partial class HomePage : ContentPage
        public HomePage()

        private void OnShare(object sender, EventArgs e)
            string 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

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: Logo

You are commenting using your 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