Adding a Share Button to a Xamarin.iOS App

Many months back, we visited the topic of adding a share button in Objective-C to your iOS app. Being able to share data easily is important to many types of Apps. As Xamarin has gained traction and become more broadly used, it makes sense to revisit this topic from the perspective of a Xamarin.iOS developer. In this article, we’ll cover the highlights of implementing a share button in a small Xamarin.iOS app.

Implementing a Share Button

Creating the share button is an easy process on Xamarin.iOS. For this example I'd recommend adding a using Foundation statement to the top of your ViewController since we'll be working with a lot of native objects. First we need to declare our shareButton object (which is of the type UIBarButtonItem just like in native iOS).

public UIBarButtonItem shareButton;  

In the ViewController.cs, we'll need to initialize the button, and add it as the right navigation item of our ViewController. ShareButton We'll also specify the ShareEventHandler (which I'll get to shortly) to handle the event that occurs when the button is clicked. This is actually a subtle difference between native iOS and Xamarin.iOS where a native selector becomes an event in C#.

shareButton = new UIBarButtonItem(UIBarButtonSystemItem.Action, ShareEventHandler);  
this.NavigationItem.RightBarButtonItem = shareButton;  

The ShareEventHandler

The next step is to handle what happens when our shareButton is clicked. This is actually pretty straightforward too. We'll use the GetImage method of our FlexChart object to return an NSData object that can be used to create a UIImage (which can easily be done here by passing it in via the constructor). This image can then be added into an array of NSObjects which we can pass into our UIActivityViewController. If we wanted to share other items we could also add it to this array (for example if we wanted to pass a string in to automatically provide a caption). Next we'll need to create a new UIActivityViewController. The UIActivityViewController is what we associate with a sharing screen. UIAlertViewController We can also exclude Activity types if we want to restrict what shows up in our UIAlertViewController as options for sharing or saving the image. Apple has a list of the builtin activityTypes if you want to exclude any in your own implementation. In this example there are no exclusions so it's simply left empty. We'll also need to handle a condition that occurs if our app is running on an iPad since that requires some extra logic. On an iPad, the sharing menu is presented as a popover which means that we'll need to handle the SourceView (which is just our ViewController represented as View in Xamarin.iOS) and the SourceRect which is where the popover will be drawn on the screen. Thus we need to determine the UserInterfaceIdiom of the current device, and make these changes if the app is running on an iPad.

 void ShareEventHandler(object sender, EventArgs e)  

            UIImage image = new UIImage(chart.GetImage());  
            NSObject[] activityItems = { image };  
            UIActivityViewController activityViewController = new UIActivityViewController(activityItems, null);  
            activityViewController.ExcludedActivityTypes = new NSString[] { };  
            if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)  
                activityViewController.PopoverPresentationController.SourceView = View;  
                activityViewController.PopoverPresentationController.SourceRect = new CoreGraphics.CGRect((View.Bounds.Width / 2), (View.Bounds.Height / 4), 0, 0);  
            this.PresentViewController(activityViewController, true, null);  

Finally, you should be sure to add any appropriate access permission for your app to the info.plist. For this sample, we'll grant it the ability to save images to the photo library by adding the Privacy - Photo Library Usage Description property with the value Save image to photos. This is especially important in iOS 10+ since you'll experience a crash if this is missing. That should finish up out implementation we should now be able to run the app. Note that the options displayed in the sharing menu will vary based on what activities are installed and available on the device the app has been installed on. ExportImage

Wrap Up

Adding a share button is actually a pretty simple process and gives your users a lot of flexibility. Not only does it make it trivial to share data via email and social media, but it also gives fast and easy access to file saving and printing. The process on Xamarin.iOS should be familiar if you're coming from native iOS, and, for strict Xamarin users, it should give you some exposure to some of the native APIs.

Download the FlexChartShare sample >>

Download Now!<%/if%>


GrapeCity Developer Tools
comments powered by Disqus