4

I'm a beginner on Xamarin ant try to create simple app with Xamarin.forms. Now i need to add share button (square + arrow like on Safari on iPhone) to my App. As I know that Android and IOS are differant so that I use DependencyService. It works good on Android using Intent but can't do it on IOS. can you help me please. thanks all, this is my code

PCL

    var x = DependencyService.Get<IShareable>(); 
    x.ShareText("any text to share"); 

Android

public void ShareText(string textToShear) 
{
  var myIntent = new Intent(Android.Content.Intent.ActionSend);      
  myIntent.SetType("text/plain"); 
  myIntent.PutExtra("sms_body", textToShear); 
  Forms.Context.StartActivity(Intent.CreateChooser(myIntent,"Choose an App"));
}

IOS

public void ShareText(string textToShear) 
{
 //what i should do 
} 
Kastour
  • 117
  • 3
  • 8

2 Answers2

2

You want to use UIActivityViewController.

It will look something like this:

public void ShareText(string textToShare) 
{
    var activityController = new UIActivityViewController(new NSObject[] { UIActivity.FromObject(textToShare) }, null);
    UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(activityController, true, null);
}
David Noël
  • 117
  • 3
1

I am in the exact same position.

What I have found out so far:

  • iOS apps typically use something called Sharekit, but I haven't found anyone using that with Xamarin.Forms
  • Since iOS 6 there is an easier way to share with objective c, but I'm not sure how we could convert that to C# / Xamarin.
  • There is a social plugin for Xamarin called Xamarin.Social that allows users to log in and post things from your app. Of course this isn't what we want to do... ideally we would like the same functionality that is in Android.

I'm sorry I couldn't be of more help. If I find an answer that works I will update you.

Primalpat
  • 374
  • 4
  • 17