iosxamarinxamarin.formsshare-button

Share button on Xamarin.forms.ios


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 
} 

Solution

  • 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);
    }