Sharing Data To Third-Party Services

Introduction

The ActivityViewController is a controller that allows the current application user to share the data in the form of text, images etc easily between the current application and services. There are number of services such as social networks, email, and sms that are provided by the operating system where we can share the data easily. 

Prerequisite

  1. Basics of C# and programming.
  2. Basics of Xamarin.

Implementation

Open Visual Studio Community.

Xamarin

Select single view app and click on next.

Xamarin

Give application name and click on next.

Xamarin

Give project name and solution name and create the application.

 Open Main.storyboard

 Add image view from toolbox.

Xamarin 

Drag the image view to your controller.

Xamarin

Set the name, image properties.

Xamarin 

Add the image (myImage.png) to your solution.

Xamarin

Add button from toolbox.

Xamarin

Add the button to your controller view.

Xamarin

Set the properties for text.

Add three buttons and set the text as given in view controller.

Xamarin

Set the text for three added buttons as (“share to anywhere”, “share to facebook”, “share to twitter”).

Now double click on buttons individually and write the code as below.

Xamarin

Locate the press enter to write your event method, you will have your event as below

Event template naem is as ButtonName_TouchUpInside().

Xamarin

Your viewController.cs file will be as below.

  1. using System;  
  2. using Social;  
  3. using UIKit;  
  4.   
  5. namespace SocialSharing {  
  6.     public partial class ViewController : UIViewController {  
  7.         protected ViewController(IntPtr handle) : base(handle) {  
  8.             // Note: this .ctor should not contain any initialization logic.  
  9.         }  
  10.   
  11.         public override void ViewDidLoad() {  
  12.             base.ViewDidLoad();  
  13.             // Perform any additional setup after loading the view, typically from a nib.  
  14.         }  
  15.   
  16.         public override void DidReceiveMemoryWarning() {  
  17.             base.DidReceiveMemoryWarning();  
  18.             // Release any cached data, images, etc that aren't in use.  
  19.         }  
  20.   
  21.         partial void Facebook_TouchUpInside(UIButton sender) {  
  22.             var text = "Sharing from c-sharpcorner.com";  
  23.             var image = MyImage.Image;  
  24.             if(SLComposeViewController.IsAvailable(SLServiceType.Facebook))  
  25.             {  
  26.                 var facebook = SLComposeViewController.FromService(SLServiceType.Facebook);  
  27.                 facebook.SetInitialText(text);  
  28.                 facebook.AddImage(image);  
  29.                 PresentViewController(facebook, true,null);  
  30.             }  
  31.         }  
  32.   
  33.         partial void Twitter_TouchUpInside(UIButton sender) {  
  34.             var text = "Sharing from c-sharpcorner.com";  
  35.             var image = MyImage.Image;  
  36.             if(SLComposeViewController.IsAvailable(SLServiceType.Twitter))  
  37.             {  
  38.                 var twitter = SLComposeViewController.FromService(SLServiceType.Twitter);  
  39.                 twitter.SetInitialText(text);  
  40.                 twitter.AddImage(image);  
  41.                 PresentViewController(twitter,true,null);  
  42.             }  
  43.         }  
  44.   
  45.         partial void Anywhere_TouchUpInside(UIButton sender) {  
  46.             var text = FromObject("Sharing from c-sharpcorner.com");  
  47.             var image = FromObject(MyImage.Image);  
  48.             var items = new[] { text, image };  
  49.             var activity = new UIActivityViewController(items, null);  
  50.             PresentViewController(activity, truenull);  
  51.         }  
  52.     }  
  53. }  

Now run the application.

You will have the following screen.

Xamarin
Click on “Share to Anywhere”.

Xamarin

If you have facebook and twitter applications installed, then you will have the facility to share the data to the particular source.

Now we have used the Facebook and Twitter button individually. These individual buttons are used to share the data directly on the defined application source. Like Facebook button will share the data to the facebook and twitter button will share the data directly to the twitter. You may have linkedin button or any other app for its source.

Up Next
    Ebook Download
    View all
    Learn
    View all