{"id":24961,"date":"2016-03-29T12:52:43","date_gmt":"2016-03-29T19:52:43","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=24961"},"modified":"2016-03-29T12:52:43","modified_gmt":"2016-03-29T19:52:43","slug":"simplify-sharing-with-plugins-for-xamarin","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/simplify-sharing-with-plugins-for-xamarin\/","title":{"rendered":"Simplify Sharing with Plugins for Xamarin"},"content":{"rendered":"<p>\t\t\t\t<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Shareplugin.png\" alt=\"NuGet package icon for the Share Plugin\" width=\"128\" height=\"128\" class=\"alignright size-full wp-image-25100\" \/>In an increasingly connected world, it&#8217;s not surprising that many apps have a social component. Even if you aren&#8217;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.<\/p>\n<p>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 <a href=\"https:\/\/www.nuget.org\/packages\/Plugin.Share\">Share Plugin for Xamarin and Windows<\/a> to implement sharing in your iOS, Android, and UWP apps in cross-platform code.<\/p>\n<h2>Introducing the Share Plugin for Xamarin and Windows<\/h2>\n<p><a href=\"https:\/\/www.xamarin.com\/plugins\">Plugins for Xamarin and Windows<\/a> provide a very easy way to access platform-specific features like  from shared code, such as <a href=\"https:\/\/blog.xamarin.com\/cross-platform-messaging-for-ios-android-and-windows\/\">messaging<\/a>, local notifications, or <a href=\"https:\/\/blog.xamarin.com\/geolocation-for-ios-android-and-windows-made-easy\/\">geolocation<\/a>, 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.<\/p>\n<p>The <a href=\"https:\/\/www.nuget.org\/packages\/Plugin.Share\/\">Share Plugin for Xamarin and Windows<\/a> provides an easy way to add cross-platform sharing to your iOS, Android, and Windows apps. To get started, simply <a href=\"https:\/\/www.nuget.org\/packages\/Plugin.Share\/\">add the plugin&#8217;s NuGet package<\/a> 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 <code>CrossShare.Current<\/code> singleton.<\/p>\n<h3>Sharing Text &amp; Links<\/h3>\n<p>To share text and links, you can use the <code>Share<\/code> and <code>ShareLink<\/code> methods of the Share Plugin for Xamarin. This will open each platform&#8217;s sharing mechanism, such as a share sheet on iOS.<\/p>\n<pre class=\"lang:csharp decode:true\">\nvar title = \"Plugins for Xamarin\";\nvar message = \"You should check out Plugins for Xamarin at xamarin.com\/plugins!\";\nvar url = \"https:\/\/www.xamarin.com\/plugins\";\n\n\/\/ Share message and an optional title.\nawait CrossShare.Current.Share(message, title);\n\n\/\/ Share a link and an optional title and message.\nawait CrossShare.Current.ShareLink(url, message, title);\n<\/pre>\n<h3>Copy Items to Clipboard<\/h3>\n<p>It&#8217;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 <code>SupportsClipboard<\/code> boolean property. Next, call the <code>SetClipboardText<\/code> method and supply the text to copy.<\/p>\n<pre class=\"lang:csharp decode:true\">\nif (CrossShare.Current.SupportsClipboard)\n{\n    var text = \"https:\/\/www.xamarin.com\/plugins\";\n    await CrossShare.Current.SetClipboardText(text);\n}\n<\/pre>\n<h3>Open a Url in the Browser<\/h3>\n<p>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 <code>CrossShare.Current.OpenBrowser<\/code> 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.<\/p>\n<p>Android provides a way to display a web page within the app with <a href=\"https:\/\/blog.xamarin.com\/keep-users-from-leaving-your-app-with-chrome-custom-tabs\/\">Chrome Custom Tabs<\/a>. Not only is this better from a UX standpoint, but it also best approach from a performance standpoint. iOS 9 introduced <code>SFSafariViewController<\/code> 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.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/safari.gif\" alt=\"Showing how SFSafariViewController loads a web page inside your app.\" width=\"195\" height=\"360\" class=\"aligncenter size-full wp-image-25104\" \/><\/p>\n<h2>Wrapping Up<\/h2>\n<p><a href=\"http:\/\/xamarin.com\/plugins\">Plugins for Xamarin and Windows<\/a> 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 <a href=\"https:\/\/github.com\/jguertl\/SharePlugin\">Share Plugin for Xamarin and Windows<\/a> 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, <a href=\"https:\/\/components.xamarin.com\/view\/SharePlugin\">visit the Share Plugin in the Xamarin Component Store<\/a> for more documentation and samples.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In an increasingly connected world, it&#8217;s not surprising that many apps have a social component. Even if you aren&#8217;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, [&hellip;]<\/p>\n","protected":false},"author":546,"featured_media":39167,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[4],"class_list":["post-24961","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>In an increasingly connected world, it&#8217;s not surprising that many apps have a social component. Even if you aren&#8217;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, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/24961","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/546"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=24961"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/24961\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/39167"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=24961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=24961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=24961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}