Xamarin Guide 5: Add ShareService

Scope

This Xamarin Workshop Guide was created for the The Portuguese National Meeting of IT Students (ENEI) by Sara Silva with the original content available here. With the goal to extend it to the global community, it was published in a new project called Xam Community Workshop with the main goal for any developer or user group to customize it to their events.

Before reading this article you must read:

Guide 5. Add ShareService

An application that allows sharing content in social networks brings more value to the users, because it allows sharing with others something the user thinks is important or relevant. An application for events like 1010 ENEI Sessions App could not miss this feature.

Each platform has its own implementation to share content in the social network. This means we need to create an abstraction of the share feature to use it in ENEI.SessionsApp and then in each platform we need to implement that abstraction.

Let's see how the Abstraction Pattern can be created!

In ENEI.SessionsApp project, create the interface IShareService as in the following:

  1. public interface IShareService    
  2. {    
  3.     void ShareLink(string title, string status, string link);    
  4. }   
Then it is required to create the implementation of the IShareService in each platform. Let's define the following implementation by platform.

Windows Phone

  1. [assembly: Dependency(typeof(ShareService))]  
  2. namespace ENEI.SessionsApp.WinPhone.Services  
  3. {  
  4.     public class ShareService : IShareService  
  5.     {  
  6.         public void ShareLink(string title, string status, string link)  
  7.         {  
  8.             var task = new ShareLinkTask { Title = title, Message = status, LinkUri = new Uri(link) };  
  9.             Device.BeginInvokeOnMainThread(() =>  
  10.             {  
  11.                 try  
  12.                 {  
  13.                     task.Show();  
  14.                 }  
  15.                 catch (Exception ex)  
  16.                 {  
  17.                  // todo handle the error     
  18.                 }  
  19.             });  
  20.         }  
  21.     }  
  22. }  
Android
  1. [assembly: Dependency(typeof(ShareService))]  
  2. namespace ENEI.SessionsApp.Droid.Services  
  3. {  
  4.     public class ShareService : IShareService  
  5.     {  
  6.         public void ShareLink(string title, string status, string link)  
  7.         {  
  8.             var intent = new Intent(global::Android.Content.Intent.ActionSend);  
  9.             intent.PutExtra(global::Android.Content.Intent.ExtraText, string.Format("{0} - {1}", status ?? string.Empty, link ?? string.Empty));  
  10.             intent.PutExtra(global::Android.Content.Intent.ExtraSubject, title ?? string.Empty);  
  11.             intent.SetType("text/plain");  
  12.             intent.SetFlags(ActivityFlags.ClearTop);  
  13.             intent.SetFlags(ActivityFlags.NewTask);  
  14.             Android.App.Application.Context.StartActivity(intent);  
  15.         }  
  16.     }  
  17. }  
iOS
  1. [assembly: Dependency(typeof(ShareService))]  
  2. namespace ENEI.SessionsApp.iOS.Services  
  3. {  
  4.     public class ShareService : IShareService  
  5.     {  
  6.         public void ShareLink(string title, string status, string link)  
  7.         {  
  8.             var actionSheet = new UIActionSheet("Share on");  
  9.             foreach (SLServiceKind service in Enum.GetValues(typeof(SLServiceKind)))  
  10.             {  
  11.                 actionSheet.AddButton(service.ToString());  
  12.             }  
  13.             actionSheet.Clicked += delegate(object a, UIButtonEventArgs b)  
  14.             {  
  15.                 SLServiceKind serviceKind = (SLServiceKind)Enum.Parse(typeof(SLServiceKind), actionSheet.ButtonTitle(b.ButtonIndex));  
  16.                 ShareOnService(serviceKind, title, status, link);  
  17.             };  
  18.             actionSheet.ShowInView(UIApplication.SharedApplication.KeyWindow.RootViewController.View);  
  19.         }  
  20.   
  21.         private void ShareOnService(SLServiceKind service, string title, string status, string link)  
  22.         {  
  23.             if (SLComposeViewController.IsAvailable(service))  
  24.             {  
  25.                 var slComposer = SLComposeViewController.FromService(service);  
  26.                 slComposer.SetInitialText(status);  
  27.                 slComposer.SetInitialText(title != null ? string.Format("{0} {1}", title, status) : status);  
  28.                 if (link != null)  
  29.                 {  
  30.                     slComposer.AddUrl(new NSUrl(link));  
  31.                 }  
  32.                 slComposer.CompletionHandler += (result) =>  
  33.                 {  
  34.                     UIApplication.SharedApplication.KeyWindow.RootViewController.InvokeOnMainThread(() =>  
  35.                     {  
  36.                         UIApplication.SharedApplication.KeyWindow.RootViewController.DismissViewController(truenull);  
  37.                     });  
  38.                 };  
  39.                 UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(slComposer, truenull);  
  40.             }  
  41.         }  
  42.     }  
  43. }  
This way, we are ready to call the share service in the SessionsView using DependencyService, as in the following:
  1. private void ShareGesture_OnTapped(object sender, EventArgs e)  
  2. {  
  3.     var tappedEventArg = e as TappedEventArgs;  
  4.     if (tappedEventArg != null)  
  5.     {  
  6.         var session = tappedEventArg.Parameter as Session;  
  7.         if (session != null)  
  8.         {  
  9.             var shareService = DependencyService.Get<IShareService>();  
  10.             if (shareService != null)  
  11.             {  
  12.                 var status = string.Format("Não percas a sessão {0} de {1}.", session.Name, session.Speaker.Name);  
  13.                 shareService.ShareLink("ENEI 2015", status, "https://enei.pt/");  
  14.             }  
  15.         }  
  16.     }  
  17. }  
The DependencyService only will get the implementation from the IShareService because the implementation from each platform was registered using:
  1. [assembly: Dependency(typeof(ShareService))]  
At this moment the 1010 ENEI Sessions App supports the sharing of a session in a social network!


Similar Articles