Simplify Sharing with Plugins for Xamarin

Pierce Boggan

NuGet package icon for the Share PluginIn an increasingly connected world, it’s not surprising that many apps have a social component. Even if you aren’t building an app that makes extensive use of social networks like Facebook, Twitter, or Instagram, you may still want a way for users to share app content with their friends on various social networks. For example, Instagram provides an easy way for you to share a photo to either Facebook or Twitter. This is a great viral tactic to get your app in front of more potential users.

Each platform provides different ways to share content, making it difficult to share code. In this blog post, you will learn how to use the Share Plugin for Xamarin and Windows to implement sharing in your iOS, Android, and UWP apps in cross-platform code.

Introducing the Share Plugin for Xamarin and Windows

Plugins for Xamarin and Windows provide a very easy way to access platform-specific features like from shared code, such as messaging, local notifications, or geolocation, making you a more productive developer. The APIs are also much simpler to work with than the platform-specific APIs in many cases, such as contacts.

The Share Plugin for Xamarin and Windows provides an easy way to add cross-platform sharing to your iOS, Android, and Windows apps. To get started, simply add the plugin’s NuGet package to both the portable class library and the individual platform projects where the plugin is used. For example, if you are using Xamarin.Forms, you would add the Share Plugin for Xamarin and Windows NuGet to your PCL, iOS, Android, and Windows projects. All APIs in the share plugin can be accessed via the CrossShare.Current singleton.

To share text and links, you can use the Share and ShareLink methods of the Share Plugin for Xamarin. This will open each platform’s sharing mechanism, such as a share sheet on iOS.

var title = "Plugins for Xamarin";
var message = "You should check out Plugins for Xamarin at!";
var url = "";

// Share message and an optional title.
await CrossShare.Current.Share(message, title);

// Share a link and an optional title and message.
await CrossShare.Current.ShareLink(url, message, title);

Copy Items to Clipboard

It’s often helpful to copy items to the clipboard for mobile users. To do this with the share plugin, first check to make sure that the mobile device supports copying items to the clipboard with the SupportsClipboard boolean property. Next, call the SetClipboardText method and supply the text to copy.

if (CrossShare.Current.SupportsClipboard)
    var text = "";
    await CrossShare.Current.SetClipboardText(text);

Open a Url in the Browser

Many mobile apps also need the ability to open a url in the browser, such as a privacy policy, terms of use, or even just linking the user to your website. You can open a browser with the Share Plugin for Xamarin and Windows by using the CrossShare.Current.OpenBrowser method. Rather than opening the native browser (such as Safari on iOS) and direct users away from your apps, the Share Plugin for Xamarin and Windows takes a more optimized approach.

Android provides a way to display a web page within the app with Chrome Custom Tabs. Not only is this better from a UX standpoint, but it also best approach from a performance standpoint. iOS 9 introduced SFSafariViewController as an easy way to show web pages from within your mobile app. Each of these behaviors is now enabled by default with using the share plugin for an optimized user experience.

Showing how SFSafariViewController loads a web page inside your app.

Wrapping Up

Plugins for Xamarin and Windows give you an easy way to add certain platform-specific functionality to shared code, saving you time and making you a more productive developer. The Share Plugin for Xamarin and Windows provides a convenient way to not only provide easy ways for users to share information from your app via social networks, email, and SMS, but also allows you to open browsers in-app and copy text to the clipboard. To get started, visit the Share Plugin in the Xamarin Component Store for more documentation and samples.


Discussion is closed.

Feedback usabilla icon