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
- Basics of C# and programming.
- Basics of Xamarin.
Implementation
Open Visual Studio Community.
Select single view app and click on next.
Give application name and click on next.
Give project name and solution name and create the application.
Open Main.storyboard
Add image view from toolbox.
Drag the image view to your controller.
Set the name, image properties.
Add the image (myImage.png) to your solution.
Add button from toolbox.
Add the button to your controller view.
Set the properties for text.
Add three buttons and set the text as given in view controller.
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.
Locate the press enter to write your event method, you will have your event as below
Event template naem is as ButtonName_TouchUpInside().
Your viewController.cs file will be as below.
- using System;
- using Social;
- using UIKit;
-
- namespace SocialSharing {
- public partial class ViewController : UIViewController {
- protected ViewController(IntPtr handle) : base(handle) {
-
- }
-
- public override void ViewDidLoad() {
- base.ViewDidLoad();
-
- }
-
- public override void DidReceiveMemoryWarning() {
- base.DidReceiveMemoryWarning();
-
- }
-
- partial void Facebook_TouchUpInside(UIButton sender) {
- var text = "Sharing from c-sharpcorner.com";
- var image = MyImage.Image;
- if(SLComposeViewController.IsAvailable(SLServiceType.Facebook))
- {
- var facebook = SLComposeViewController.FromService(SLServiceType.Facebook);
- facebook.SetInitialText(text);
- facebook.AddImage(image);
- PresentViewController(facebook, true,null);
- }
- }
-
- partial void Twitter_TouchUpInside(UIButton sender) {
- var text = "Sharing from c-sharpcorner.com";
- var image = MyImage.Image;
- if(SLComposeViewController.IsAvailable(SLServiceType.Twitter))
- {
- var twitter = SLComposeViewController.FromService(SLServiceType.Twitter);
- twitter.SetInitialText(text);
- twitter.AddImage(image);
- PresentViewController(twitter,true,null);
- }
- }
-
- partial void Anywhere_TouchUpInside(UIButton sender) {
- var text = FromObject("Sharing from c-sharpcorner.com");
- var image = FromObject(MyImage.Image);
- var items = new[] { text, image };
- var activity = new UIActivityViewController(items, null);
- PresentViewController(activity, true, null);
- }
- }
- }
Now run the application.
You will have the following screen.
Click on “Share to Anywhere”.
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.