One of the key points to make our app grow in users is to get spread through social networks: WhatsApp, Facebook, Twitter, and so on. In this tutorial we are going to learn how to place a native share button in our apps.

  • Encourage your users to share your app content.
  • Make it easy for them to tell their close people about your app.
  • Convert shares in new shares.
  • Reach new people.

front

Ingredients

  • Visual Studio updated.
  • Something to share!

Steps

  1. Creating a simple app
  2. Designing a Dependency injection
    1. Implementig on Android
    2. Implementig on iOS

Code!

XamarinSocialExample

1. Creating a simple app

An exciting beging, creat a Xamarin.Forms blank app! fc2acd3

I will soon upload a tutorial on how to clean your app for the initial commit.

I created a button that will trigger the Social popup. This button calls the Share method. Just for a minute, I will display an alert.

2. Designing a Dependency injection

Create a new interface in your shared project. This will be the DependencyInjection interface.

namespace XamarinSocial
{
	public interface ISocial
	{
		void Share(string content);
	}
}

Now we are going to implement the DependencyInjection in both devices. It’s just creating a ShareService.cs class that contains the following:

2.1. Implementig on Android b9f2f51

using Android.Content;
using Xamarin.Forms;
using XamarinSocial.Droid;

[assembly: Dependency(typeof(SocialService))]
namespace XamarinSocial.Droid
{
	public class SocialService : ISocial
    {      
		public void Share(string content)
		{         
			Intent share = new Intent(Intent.ActionSend)
				.SetType("text/plain")
				.AddFlags(ActivityFlags.ClearWhenTaskReset)
				.PutExtra(Intent.ExtraText, content);

			Android.App.Application.Context.StartActivity(Intent.CreateChooser(share, "Compartir!"));
		}
	}
}

2.2. Implementig on iOS c8a18df

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

[assembly: Dependency(typeof(SocialService))]
namespace XamarinSocial.iOS
{
	public class SocialService : ISocial
	{
		public void Share(string content)
		{
			var activityViewController = new UIActivityViewController(new NSString[] { new NSString(content) }, null);

			UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(activityViewController, true, null);
		}
	}
}