You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

531 lines
33 KiB

  1. # PhoneGap Social Sharing plugin for Android, iOS and Windows Phone
  2. by [@EddyVerbruggen](http://www.twitter.com/eddyverbruggen), [read my blog about this plugin](http://www.x-services.nl/phonegap-share-plugin-facebook-twitter-social-media/754)
  3. [![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=eddyverbruggen%40gmail%2ecom&lc=US&item_name=cordova%2dplugin%2dsocialsharing&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHosted)
  4. Every now and then kind folks ask me how they can give me all their money. So if you want to contribute to my pension fund, then please go ahead :)
  5. <table width="100%">
  6. <tr>
  7. <td width="100"><a href="http://plugins.telerik.com/plugin/socialsharing"><img src="http://www.x-services.nl/github-images/telerik-verified-plugins-marketplace.png" width="97px" height="71px" alt="Marketplace logo"/></a></td>
  8. <td>For a quick demo app and easy code samples, check out the plugin page at the Verified Plugins Marketplace: http://plugins.telerik.com/plugin/socialsharing</td>
  9. </tr>
  10. </table>
  11. ## 0. Index
  12. 1. [Description](#1-description)
  13. 2. [Screenshots](#2-screenshots)
  14. 3. [Installation](#3-installation)
  15. 3. [Automatically (CLI / Plugman)](#automatically-cli--plugman)
  16. 3. [Manually](#manually)
  17. 3. [PhoneGap Build](#phonegap-build)
  18. 4. Usage
  19. 4. [iOS and Android](#4a-usage-on-ios-and-android)
  20. 4. [Windows Phone](#4b-usage-on-windows-phone)
  21. 4. [Share-popover on iPad](#4c-share-popover-on-ipad)
  22. 4. [Whitelisting on iOS 9](#4d-whitelisting-on-ios-9)
  23. 5. [Credits](#5-credits)
  24. 6. [License](#6-license)
  25. ## 1. Description
  26. This plugin allows you to use the native sharing window of your mobile device.
  27. * Works on Android, version 2.3.3 and higher (probably 2.2 as well).
  28. * Works on iOS6 and up.
  29. * Works on Windows Phone 8 since v4.0 of this plugin (maybe even WP7, but I have no such testdevice).
  30. * Share text, a link, a images (or other files like pdf or ics). Subject is also supported, when the receiving app supports it.
  31. * Supports sharing files from the internet, the local filesystem, or from the www folder.
  32. * You can skip the sharing dialog and directly share to Twitter, Facebook, or other apps.
  33. * Compatible with [Cordova Plugman](https://github.com/apache/cordova-plugman).
  34. * Officially supported by [PhoneGap Build](https://build.phonegap.com/plugins).
  35. ## 2. Screenshots
  36. iOS 7 (iPhone)
  37. ![ScreenShot](https://raw.githubusercontent.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin/master/screenshots/screenshot-ios7-share.png)
  38. Sharing options are based on what has been setup in the device settings
  39. ![ScreenShot](https://raw.githubusercontent.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin/master/screenshots/screenshots-ios7-shareconfig.png)
  40. iOS 7 (iPad) - a popup like this requires [a little more effort](#4c-share-popover-on-ipad)
  41. ![ScreenShot](https://raw.githubusercontent.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin/master/screenshots/screenshot-ios7-ipad-share.png)
  42. iOS 6 (iPhone)
  43. ![ScreenShot](https://raw.githubusercontent.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin/master/screenshots/screenshot-ios6-share.png)
  44. Android
  45. ![ScreenShot](https://raw.githubusercontent.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin/master/screenshots/screenshot-android-share.png)
  46. Windows Phone 8
  47. ![ScreenShot](https://raw.githubusercontent.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin/master/screenshots/screenshot-wp8-share.jpg)
  48. #### Alternative ShareSheet (iOS only, using the [Cordova ActionSheet plugin](https://github.com/EddyVerbruggen/cordova-plugin-actionsheet))
  49. ![ScreenShot](https://raw.githubusercontent.com/EddyVerbruggen/cordova-plugin-actionsheet/master/screenshots/ios/ios-share.png)
  50. ## 3. Installation
  51. ### Automatically (CLI / Plugman)
  52. SocialSharing is compatible with [Cordova Plugman](https://github.com/apache/cordova-plugman), compatible with [PhoneGap 3.0 CLI](http://docs.phonegap.com/en/3.0.0/guide_cli_index.md.html#The%20Command-line%20Interface_add_features), here's how it works with the CLI:
  53. ```
  54. $ phonegap local plugin add https://github.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin.git
  55. ```
  56. or with Cordova CLI, from npm:
  57. ```
  58. $ cordova plugin add cordova-plugin-x-socialsharing
  59. $ cordova prepare
  60. ```
  61. SocialSharing.js is brought in automatically. There is no need to change or add anything in your html.
  62. ### Manually
  63. 1\. Add the following xml to all the `config.xml` files you can find:
  64. ```xml
  65. <!-- for iOS -->
  66. <feature name="SocialSharing">
  67. <param name="ios-package" value="SocialSharing" />
  68. </feature>
  69. ```
  70. ```xml
  71. <!-- for Android (you will find one in res/xml) -->
  72. <feature name="SocialSharing">
  73. <param name="android-package" value="nl.xservices.plugins.SocialSharing" />
  74. </feature>
  75. ```
  76. ```xml
  77. <!-- for Windows Phone -->
  78. <feature name="SocialSharing">
  79. <param name="wp-package" value="SocialSharing"/>
  80. </feature>
  81. ```
  82. For sharing remote images (or other files) on Android, the file needs to be stored locally first, so add this permission to `AndroidManifest.xml`:
  83. ```xml
  84. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  85. ```
  86. For iOS, you'll need to add the `Social.framework` and `MessageUI.framework` to your project. Click your project, Build Phases, Link Binary With Libraries, search for and add `Social.framework` and `MessageUI.framework`.
  87. 2\. Grab a copy of SocialSharing.js, add it to your project and reference it in `index.html`:
  88. ```html
  89. <script type="text/javascript" src="js/SocialSharing.js"></script>
  90. ```
  91. 3\. Download the source files for iOS and/or Android and copy them to your project.
  92. iOS: Copy `SocialSharing.h` and `SocialSharing.m` to `platforms/ios/<ProjectName>/Plugins`
  93. Android: Copy `SocialSharing.java` to `platforms/android/src/nl/xservices/plugins` (create the folders)
  94. Window Phone: Copy `SocialSharing.cs` to `platforms/wp8/Plugins/nl.x-services.plugins.socialsharing` (create the folders)
  95. ### PhoneGap Build
  96. Just add the following xml to your `config.xml` to always use the latest version of this plugin (which is published to plugins.cordova.io these days):
  97. ```xml
  98. <gap:plugin name="cordova-plugin-x-socialsharing" source="npm" />
  99. ```
  100. or to use an older version, hosted at phonegap build:
  101. ```xml
  102. <gap:plugin name="nl.x-services.plugins.socialsharing" version="4.3.16" />
  103. ```
  104. SocialSharing.js is brought in automatically. Make sure though you include a reference to cordova.js in your index.html's head:
  105. ```html
  106. <script type="text/javascript" src="cordova.js"></script>
  107. ```
  108. ## 4a. Usage on iOS and Android
  109. You can share text, a subject (in case the user selects the email application), (any type and location of) file (like an image), and a link.
  110. However, what exactly gets shared, depends on the application the user chooses to complete the action. A few examples:
  111. - Mail: message, subject, file.
  112. - Twitter: message, image (other filetypes are not supported), link (which is automatically shortened if the Twitter client deems it necessary).
  113. - Google+ / Hangouts (Android only): message, subject, link
  114. - Flickr: message, image (an image is required for this option to show up).
  115. - Facebook Android: sharing a message is not possible. You can share either a link or an image (not both), but a description can not be prefilled. See [this Facebook issue which they won't solve](https://developers.facebook.com/x/bugs/332619626816423/). As an alternative you can use `shareViaFacebookWithPasteMessageHint` since plugin version 4.3.4. See below for details. Also note that sharing a URL on a non standard domain (like .fail) [may not work on Android](https://github.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin/issues/253). Make sure you test this. You can use a [link shortener](https://goo.gl) to workaround this issue.
  116. - Facebook iOS: message, image (other filetypes are not supported), link. Beware that since a Fb update in April 2015 sharing a prefilled message is no longer possible when the Fb app is installed (like Android), see #344. Alternative: use `shareViaFacebookWithPasteMessageHint`.
  117. ### Using the share sheet
  118. Since version 5.1.0 (for iOS and Android) it's recommended to use `shareWithOptions` as it's the most feature rich way to share stuff cross-platform.
  119. It will also tell you if sharing to an app completed and which app that was (if that app plays nice, that is).
  120. ```js
  121. // this is the complete list of currently supported params you can pass to the plugin (all optional)
  122. var options = {
  123. message: 'share this', // not supported on some apps (Facebook, Instagram)
  124. subject: 'the subject', // fi. for email
  125. files: ['', ''], // an array of filenames either locally or remotely
  126. url: 'https://www.website.com/foo/#bar?a=b',
  127. chooserTitle: 'Pick an app' // Android only, you can override the default share sheet title
  128. }
  129. var onSuccess = function(result) {
  130. console.log("Share completed? " + result.completed); // On Android apps mostly return false even while it's true
  131. console.log("Shared to app: " + result.app); // On Android result.app is currently empty. On iOS it's empty when sharing is cancelled (result.completed=false)
  132. }
  133. var onError = function(msg) {
  134. console.log("Sharing failed with message: " + msg);
  135. }
  136. window.plugins.socialsharing.shareWithOptions(options, onSuccess, onError);
  137. ```
  138. #### You can still use the older `share` method as well
  139. Here are some examples you can copy-paste to test the various combinations:
  140. ```html
  141. <button onclick="window.plugins.socialsharing.share('Message only')">message only</button>
  142. <button onclick="window.plugins.socialsharing.share('Message and subject', 'The subject')">message and subject</button>
  143. <button onclick="window.plugins.socialsharing.share(null, null, null, 'http://www.x-services.nl')">link only</button>
  144. <button onclick="window.plugins.socialsharing.share('Message and link', null, null, 'http://www.x-services.nl')">message and link</button>
  145. <button onclick="window.plugins.socialsharing.share(null, null, 'https://www.google.nl/images/srpr/logo4w.png', null)">image only</button>
  146. // Beware: passing a base64 file as 'data:' is not supported on Android 2.x: https://code.google.com/p/android/issues/detail?id=7901#c43
  147. // Hint: when sharing a base64 encoded file on Android you can set the filename by passing it as the subject (second param)
  148. <button onclick="window.plugins.socialsharing.share(null, 'Android filename', 'data:image/png;base64,R0lGODlhDAAMALMBAP8AAP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAUKAAEALAAAAAAMAAwAQAQZMMhJK7iY4p3nlZ8XgmNlnibXdVqolmhcRQA7', null)">base64 image only</button>
  149. // Hint: you can share multiple files by using an array as thirds param: ['file 1','file 2', ..], but beware of this Android Kitkat Facebook issue: [#164]
  150. <button onclick="window.plugins.socialsharing.share('Message and image', null, 'https://www.google.nl/images/srpr/logo4w.png', null)">message and image</button>
  151. <button onclick="window.plugins.socialsharing.share('Message, image and link', null, 'https://www.google.nl/images/srpr/logo4w.png', 'http://www.x-services.nl')">message, image and link</button>
  152. <button onclick="window.plugins.socialsharing.share('Message, subject, image and link', 'The subject', 'https://www.google.nl/images/srpr/logo4w.png', 'http://www.x-services.nl')">message, subject, image and link</button>
  153. ```
  154. Example: share a PDF file from the local www folder:
  155. ```html
  156. <button onclick="window.plugins.socialsharing.share('Here is your PDF file', 'Your PDF', 'www/files/manual.pdf')">Share PDF</button>
  157. ```
  158. ### Sharing directly to..
  159. ####Twitter
  160. ```html
  161. <!-- unlike most apps Twitter doesn't like it when you use an array to pass multiple files as the second param -->
  162. <button onclick="window.plugins.socialsharing.shareViaTwitter('Message via Twitter')">message via Twitter</button>
  163. <button onclick="window.plugins.socialsharing.shareViaTwitter('Message and link via Twitter', null /* img */, 'http://www.x-services.nl')">msg and link via Twitter</button>
  164. ```
  165. ####Facebook
  166. ```html
  167. <button onclick="window.plugins.socialsharing.shareViaFacebook('Message via Facebook', null /* img */, null /* url */, function() {console.log('share ok')}, function(errormsg){alert(errormsg)})">msg via Facebook (with errcallback)</button>
  168. ```
  169. Facebook with prefilled message - as a workaround for [this Facebook (Android) bug](https://developers.facebook.com/x/bugs/332619626816423/). BEWARE: it's against Facebooks policy to prefil the message, or even hint the user to prefill a message for them. [See this page for details.](https://developers.facebook.com/docs/apps/review/prefill)
  170. * On Android the user will see a Toast message with a message you control (default: "If you like you can paste a message from your clipboard").
  171. * On iOS this function used to behave the same as `shareViaFacebook`, but since 4.3.18 a short message is shown prompting the user to paste a message (like Android). This message is not shown in case the Fb app is not installed since the internal iOS Fb share widget still supports prefilling the message.
  172. * iOS9 quirk: if you want to use this method, you need to whitelist `fb://` in your .plist file.
  173. ```html
  174. <button onclick="window.plugins.socialsharing.shareViaFacebookWithPasteMessageHint('Message via Facebook', null /* img */, null /* url */, 'Paste it dude!', function() {console.log('share ok')}, function(errormsg){alert(errormsg)})">msg via Facebook (with errcallback)</button>
  175. ```
  176. Whitelisting Facebook in your app's .plist:
  177. ```xml
  178. <key>LSApplicationQueriesSchemes</key>
  179. <array>
  180. <string>fb</string>
  181. </array>
  182. ```
  183. ####Instagram
  184. ```html
  185. <button onclick="window.plugins.socialsharing.shareViaInstagram('Message via Instagram', 'https://www.google.nl/images/srpr/logo4w.png', function() {console.log('share ok')}, function(errormsg){alert(errormsg)})">msg via Instagram</button>
  186. ```
  187. Quirks:
  188. * Instagram no longer permits prefilling a message - you can prompt the user to paste the message you've passed to the plugin because we're adding it to the clipboard for you.
  189. iOS Quirks:
  190. * Before trying to invoke `shareViaInstagram` please use `canShareVia('instagram'..` AND whitelist the urlscheme (see below).
  191. * Although this plugin follows the Instagram sharing guidelines, the user may not only see Instagram in the share sheet, but also other apps that listen to the "Instagram sharing ID". Just google "com.instagram.exclusivegram" and you see what I mean.
  192. #### WhatsApp
  193. * Note that on iOS when sharing an image and text, only the image is shared - let's hope WhatsApp creates a proper iOS extension to fix this.
  194. * Before using this method you may want to use `canShareVia('whatsapp'..` (see below).
  195. ```html
  196. <button onclick="window.plugins.socialsharing.shareViaWhatsApp('Message via WhatsApp', null /* img */, null /* url */, function() {console.log('share ok')}, function(errormsg){alert(errormsg)})">msg via WhatsApp (with errcallback)</button>
  197. ```
  198. ##### Experimental feature: sharing directly to someone
  199. Available in 5.0.8 and up - please let me know if this works for your device! Open an issue if not..
  200. ```html
  201. <button onclick="window.plugins.socialsharing.shareViaWhatsAppToReceiver(receiver, 'Message via WhatsApp', null /* img */, null /* url */, function() {console.log('share ok')})">msg via WhatsApp for Addressbook ID 101</button>
  202. ```
  203. For `receiver` on iOS pass in the Addressbook ID (or 'abid'). You can find those abid's by using the [Cordova Contacts Plugin](https://github.com/apache/cordova-plugin-contacts).
  204. The result in the success callback of the `find` function is a JSON array of contact objects, use the 'id' you find in those objects.
  205. Don't pass in an image on iOS because that can't be sent to someone directly unfortunately. Message and URL are fine though.
  206. On Android pass in the phone number of the person you want to send a message to (untested at the moment).
  207. ####SMS
  208. Note that on Android, SMS via Hangouts may not behave correctly
  209. ```html
  210. <!-- Want to share a prefilled SMS text? -->
  211. <button onclick="window.plugins.socialsharing.shareViaSMS('My cool message', null /* see the note below */, function(msg) {console.log('ok: ' + msg)}, function(msg) {alert('error: ' + msg)})">share via SMS</button>
  212. <!-- Want to prefill some phonenumbers as well? Pass this instead of null. Important notes: For stable usage of shareViaSMS on Android 4.4 and up you require to add at least one phonenumber! Also, on Android make sure you use v4.0.3 or higher of this plugin, otherwise sharing multiple numbers to non-Samsung devices will fail -->
  213. <button onclick="window.plugins.socialsharing.shareViaSMS('My cool message', '0612345678,0687654321', function(msg) {console.log('ok: ' + msg)}, function(msg) {alert('error: ' + msg)})">share via SMS</button>
  214. <!-- Need a subject and even image sharing? It's only supported on iOS for now and falls back to just message sharing on Android -->
  215. <button onclick="window.plugins.socialsharing.shareViaSMS({'message':'My cool message', 'subject':'The subject', 'image':'https://www.google.nl/images/srpr/logo4w.png'}, '0612345678,0687654321', function(msg) {console.log('ok: ' + msg)}, function(msg) {alert('error: ' + msg)})">share via SMS</button>
  216. ```
  217. ####Email
  218. Code inspired by the [EmailComposer plugin](https://github.com/katzer/cordova-plugin-email-composer), note that this is not supported on the iOS 8 simulator (an alert will be shown if your try to).
  219. ```js
  220. window.plugins.socialsharing.shareViaEmail(
  221. 'Message', // can contain HTML tags, but support on Android is rather limited: http://stackoverflow.com/questions/15136480/how-to-send-html-content-with-image-through-android-default-email-client
  222. 'Subject',
  223. ['to@person1.com', 'to@person2.com'], // TO: must be null or an array
  224. ['cc@person1.com'], // CC: must be null or an array
  225. null, // BCC: must be null or an array
  226. ['https://www.google.nl/images/srpr/logo4w.png','www/localimage.png'], // FILES: can be null, a string, or an array
  227. onSuccess, // called when sharing worked, but also when the user cancelled sharing via email. On iOS, the callbacks' boolean result parameter is true when sharing worked, false if cancelled. On Android, this parameter is always true so it can't be used). See section "Notes about the successCallback" below.
  228. onError // called when sh*t hits the fan
  229. );
  230. ```
  231. If Facebook, Twitter, Instagram, WhatsApp, SMS or Email is not available, the errorCallback is called with the text 'not available'.
  232. If you feel lucky, you can even try to start any application with the `shareVia` function:
  233. ```html
  234. <!-- start facebook on iOS (same as `shareViaFacebook`), if Facebook is not installed, the errorcallback will be invoked with message 'not available' -->
  235. <button onclick="window.plugins.socialsharing.shareVia('com.apple.social.facebook', 'Message via FB', null, null, null, function(){console.log('share ok')}, function(msg) {alert('error: ' + msg)})">message via Facebook</button>
  236. <!-- start facebook on Android (same as `shareViaFacebook`), if Facebook is not installed, the errorcallback will be invoked with message 'not available' -->
  237. <button onclick="window.plugins.socialsharing.shareVia('facebook', 'Message via FB', null, null, null, function(){console.log('share ok')}, function(msg) {alert('error: ' + msg)})">message via Facebook</button>
  238. <!-- start twitter on iOS (same as `shareViaTwitter`), if Twitter is not installed, the errorcallback will be invoked with message 'not available' -->
  239. <button onclick="window.plugins.socialsharing.shareVia('com.apple.social.twitter', 'Message via Twitter', null, null, 'http://www.x-services.nl', function(){console.log('share ok')}, function(msg) {alert('error: ' + msg)})">message and link via Twitter on iOS</button>
  240. <!-- if you share to a non existing/supported app, the errorcallback will be invoked with message 'not available' -->
  241. <button onclick="window.plugins.socialsharing.shareVia('bogus_app', 'Message via Bogus App', null, null, null, function(){console.log('share ok')}, function(msg) {alert('error: ' + msg)})">message via Bogus App</button>
  242. ```
  243. What can we pass to the `shareVia` function?
  244. * iOS: You are limited to 'com.apple.social.[facebook | twitter | sinaweibo | tencentweibo]'. If an app does not exist, the errorcallback is invoked and iOS shows a popup message asking the user to configure the app.
  245. * Android: Anything that would otherwise appear in the sharing dialoge (in case the `share` function was used. Pass a (part of the) packagename of the app you want to share to. The `shareViaFacebook` function for instance uses `com.facebook.katana` as the packagename fragment. Things like `weibo`, `pinterest` and `com.google.android.apps.plus` (Google+) should work just fine.
  246. You can even test if a sharing option is available with `canShareVia`!
  247. You'll need to pass everything you want to share, because (at least on Android) some apps may only become available when an image is added.
  248. The function will invoke the successCallback when it can be shared to via `shareVia`, and the errorCallback if not. As a bonus on Android, the errorCallback contains a JSON Array of available packages you can pass to shareVia.
  249. You can even specify the activity if the app offers multiple sharing ways, passing 'packageName/activityName'. (for example, WeChat, passing 'com.tencent.mm' or 'com.tencent.mm/com.tencent.mm.ui.tools.ShareImgUI' to share to chat, passing 'com.tencent.mm/com.tencent.mm.ui.tools.ShareToTimeLineUI' to share to moments).
  250. ```html
  251. <button onclick="window.plugins.socialsharing.canShareVia('com.tencent.mm/com.tencent.mm.ui.tools.ShareToTimeLineUI', 'msg', null, img, null, function(e){alert(e)}, function(e){alert(e)})">is WeChat available on Android?</button>
  252. <button onclick="window.plugins.socialsharing.canShareVia('com.apple.social.facebook', 'msg', null, null, null, function(e){alert(e)}, function(e){alert(e)})">is facebook available on iOS?</button>
  253. // this one requires whitelisting of whatsapp:// on iOS9 in your .plist file
  254. <button onclick="window.plugins.socialsharing.canShareVia('whatsapp', 'msg', null, null, null, function(e){alert(e)}, function(e){alert(e)})">is WhatsApp available?</button>
  255. <button onclick="window.plugins.socialsharing.canShareVia('sms', 'msg', null, null, null, function(e){alert(e)}, function(e){alert(e)})">is SMS available?</button>
  256. <button onclick="window.plugins.socialsharing.canShareVia('instagram', 'msg', null, null, null, function(e){alert(e)}, function(e){alert(e)})">is Instagram available?</button>
  257. <!-- Email is a different beast, so I added a specific method for it -->
  258. <button onclick="window.plugins.socialsharing.canShareViaEmail(function(e){alert(e)}, function(e){alert(e)})">is Email available?</button>
  259. ```
  260. Want to share images from a local folder (like an image you just selected from the CameraRoll)?
  261. ```javascript
  262. // use a local image from inside the www folder:
  263. window.plugins.socialsharing.share(null, null, 'www/image.gif', null); // success/error callback params may be added as 5th and 6th param
  264. // .. or a local image from anywhere else (if permitted):
  265. // local-iOS:
  266. window.plugins.socialsharing.share(null, null, '/Users/username/Library/Application Support/iPhone/6.1/Applications/25A1E7CF-079F-438D-823B-55C6F8CD2DC0/Documents/.nl.x-services.appname/pics/img.jpg');
  267. // local-iOS-alt:
  268. window.plugins.socialsharing.share(null, null, 'file:///Users/username/Library/Application Support/iPhone/6.1/Applications/25A1E7CF-079F-438D-823B-55C6F8CD2DC0/Documents/.nl.x-services.appname/pics/img.jpg');
  269. // local-Android:
  270. window.plugins.socialsharing.share(null, null, 'file:///storage/emulated/0/nl.xservices.testapp/5359/Photos/16832/Thumb.jpg');
  271. // .. or an image from the internet:
  272. window.plugins.socialsharing.share(null, null, 'http://domain.com/image.jpg');
  273. ```
  274. If your app still supports iOS5, you'll want to check whether or not the plugin is available as it only supports iOS6 and up.
  275. ```javascript
  276. window.plugins.socialsharing.available(function(isAvailable) {
  277. // the boolean is only false on iOS < 6
  278. if (isAvailable) {
  279. // now use any of the share() functions
  280. }
  281. });
  282. ```
  283. If you can't get the plugin to work, have a look at [this demo project](https://github.com/EddyVerbruggen/X-Services-PhoneGap-Build-Plugins-Demo).
  284. #### Notes about the successCallback (you can just ignore the callbacks if you like)
  285. Since version 3.8 the plugin passes a boolean to the successCallback to let the app know whether or not content was actually shared, or the share widget was closed by the user.
  286. On iOS this works as expected (except for Facebook, in case the app is installed), but on Android some sharing targets may return false, even though sharing succeeded. This is not a limitation of the plugin, it's the target app which doesn't play nice.
  287. To make it more confusing, when sharing via SMS on Android, you'll likely always have the successCallback invoked. Thanks Google.
  288. #### Sharing multiple images (or other files)
  289. Since version 4.3.0 of this plugin you can pass an array of files to the share and shareVia functions.
  290. ```js
  291. // sharing multiple images via Facebook (you can mix protocols and file locations)
  292. window.plugins.socialsharing.shareViaFacebook(
  293. 'Optional message, may be ignored by Facebook app',
  294. ['https://www.google.nl/images/srpr/logo4w.png','www/image.gif'],
  295. null);
  296. // sharing a PDF and an image
  297. window.plugins.socialsharing.share(
  298. 'Optional message',
  299. 'Optional title',
  300. ['www/manual.pdf','https://www.google.nl/images/srpr/logo4w.png'],
  301. 'http://www.myurl.com');
  302. ```
  303. Note that a lot of apps support sharing multiple files, but Twitter just doesn't accept more that one file.
  304. #### Saving images to the photo album (iOS only currently)
  305. Since version 4.3.16 of this plugin you can save an array of images to the camera roll:
  306. ```js
  307. window.plugins.socialsharing.saveToPhotoAlbum(
  308. ['https://www.google.nl/images/srpr/logo4w.png','www/image.gif'],
  309. onSuccess, // optional success function
  310. onError // optional error function
  311. );
  312. ```
  313. #### iOS quirk (with camera plugin)
  314. When using this plugin in the callback of the Phonegap camera plugin, wrap the call to `share()` in a `setTimeout()`.
  315. The share widget has the same limitation as the alert dialogue [mentioned in the Phonegap documentation](http://docs.phonegap.com/en/2.9.0/cordova_camera_camera.md.html#camera.getPicture_ios_quirks).
  316. #### Excluding some options from the widget
  317. If you want to exclude (for example) the assign-to-contact and copy-to-pasteboard options, add this to your main plist file:
  318. ```xml
  319. <key>SocialSharingExcludeActivities</key>
  320. <array>
  321. <string>com.apple.UIKit.activity.AssignToContact</string>
  322. <string>com.apple.UIKit.activity.CopyToPasteboard</string>
  323. </array>
  324. ```
  325. Here's the list of available activities you can disable :
  326. - com.apple.UIKit.activity.PostToFacebook
  327. - com.apple.UIKit.activity.PostToTwitter
  328. - com.apple.UIKit.activity.PostToFlickr
  329. - com.apple.UIKit.activity.PostToWeibo
  330. - com.apple.UIKit.activity.PostToVimeo
  331. - com.apple.UIKit.activity.TencentWeibo
  332. - com.apple.UIKit.activity.Message
  333. - com.apple.UIKit.activity.Mail
  334. - com.apple.UIKit.activity.Print
  335. - com.apple.UIKit.activity.CopyToPasteboard
  336. - com.apple.UIKit.activity.AssignToContact
  337. - com.apple.UIKit.activity.SaveToCameraRoll
  338. - com.apple.UIKit.activity.AddToReadingList
  339. - com.apple.UIKit.activity.AirDrop
  340. ## 4b. Usage on Windows Phone
  341. The available methods on WP8 are: `available`, `canShareViaEmail`, `share`, `shareViaEmail` and `shareViaSMS`.
  342. Currently the first two always return true, but this may change in the future in case I can find a way to truly detect the availability.
  343. The `share` function on WP8 supports two flavours: message only, or a combination of message, title and link.
  344. Beware: for now please pass null values for all non used attributes, like in the examples below.
  345. Sharing a message:
  346. ```html
  347. <button onclick="window.plugins.socialsharing.share('Message only', null, null, null)">message only</button>
  348. ```
  349. Sharing a link:
  350. ```html
  351. <button onclick="window.plugins.socialsharing.share('Optional message', 'Optional title', null, 'http://www.x-services.nl')">message, title, link</button>
  352. ```
  353. Sharing an image (only images from the internet are supported). If you pass more than one image as an array, only the first one is used:
  354. ```html
  355. <button onclick="window.plugins.socialsharing.share('Optional message', 'Optional title', 'https://www.google.nl/images/srpr/logo4w.png', null)">image only</button>
  356. ```
  357. ## 4c. Share-popover on iPad
  358. Carlos Sola-Llonch, a user of this plugin, pointed me at an [iOS document](https://developer.apple.com/library/ios/documentation/uikit/reference/UIActivityViewController_Class/Reference/Reference.html)
  359. stating "On iPad, you must present the view controller in a popover. On iPhone and iPod touch, you must present it modally."
  360. He also provided me with the required code to do so (thanks!). I've adapted it a little to make sure current behaviour is
  361. not altered, but with a little extra effort you can use this new popover feature.
  362. The trick is overriding the function `window.plugins.socialsharing.iPadPopupCoordinates` by your own implementation
  363. to tell the iPad where to show the popup exactly. It need to be a string like "100,200,300,300" (left,top,width,height).
  364. You need to override this method after `deviceready` has fired.
  365. You have various options, like checking the click event on a button and determine the event.clientX and event.clientY,
  366. or use this code Carlos showed me to grab the coordinates of a static button somewhere on your page:
  367. ```js
  368. window.plugins.socialsharing.iPadPopupCoordinates = function() {
  369. var rect = document.getElementById('share_button').getBoundingClientRect();
  370. return rect.left + "," + rect.top + "," + rect.width + "," + rect.height;
  371. };
  372. ```
  373. Note that since iOS 8 this popup is the only way Apple allows you to share stuff, so this plugin has been adjusted to use this plugin as standard for iOS 8 and positions
  374. the popup at the bottom of the screen (seems like a logical default because that's where it previously was as well).
  375. You can however override this position in the same way as explained above.
  376. **Note**: when using the [WkWebView polyfill](https://github.com/Telerik-Verified-Plugins/WKWebView) the `iPadPopupCoordinates` overrides [doesn't work](https://github.com/Telerik-Verified-Plugins/WKWebView/issues/77) so you can call the alternative `setIPadPopupCoordinates` method to define the popup position just before you call the `share` method.
  377. example :
  378. ```js
  379. var targetRect = event.targetElement.getBoundingClientRect(),
  380. targetBounds = targetRect.left + ',' + targetRect.top + ',' + targetRect.width + ',' + targetRect.height;
  381. window.plugins.socialsharing.setIPadPopupCoordinates(targetBounds);
  382. window.plugins.socialsharing.share('Hello from iOS :)')
  383. ```
  384. ## 4d. Whitelisting on iOS 9
  385. On iOS 9 you have to make sure to whitelist the applications you want to use for sharing. Without whitelisting "query schemes", you may get the error callback invoked when calling the `canShareVia` function (and possibly the `shareVia`). You can verify this is a permissions issue by observing the output in XCode for something like:
  386. > -canOpenURL: failed for URL: "whatsapp://app" - error: "This app is not allowed to query for scheme whatsapp"
  387. You have a few options to prevent this by whitelisting the application you want to share via:
  388. ### Directly editing the .plist file
  389. This is a stright forward approach - you just manually edit the .plist file - either from within XCode or using a text editor. You can see example entries above (e.g. xyz). While this is simple to do, the changes may be lost when rebuilding the project or tweaking the platform (e.g. upgrading) and is less recomended.
  390. ### Use query schema plugin
  391. There is a plugin designed specifically to address query schema whitelisting. You can find the plugin and how to use it [here](https://www.npmjs.com/package/cordova-plugin-queries-schemes). In general, after installation, you can change plugin.xml file under the plugin subfolder within the plugins directory of your project to add the required schemas. Here again though, you have to edit an additional file and should take care not to overwrite it when making changes to your project.
  392. ### Use Custom Config plugin
  393. The Custom Config plugin ([here](https://github.com/dpa99c/cordova-custom-config)) allows you to add configuration to your platforms "native" configuration files (e.g. .plist or AndroidManifest.xml) through the project's main config.xml file.
  394. To address query schema issue, after installaing the plugin you can edit the iOS platform section of your config.xml (in the project main folder) to include the required entries:
  395. ```xml
  396. <?xml version='1.0' encoding='utf-8'?>
  397. <widget id="your.app.id"
  398. version="0.9.1"
  399. xmlns="http://www.w3.org/ns/widgets"
  400. xmlns:cdv="http://cordova.apache.org/ns/1.0">
  401. <!-- a bunch of elements like name, description etc -->
  402. <platform name="ios">
  403. <!-- add this entry -->
  404. <config-file platform="ios" target="*-Info.plist" parent="LSApplicationQueriesSchemes">
  405. <array>
  406. <string>whatsapp</string>
  407. <!-- add more query scheme strings -->
  408. </array>
  409. </config-file>
  410. </platform>
  411. </widget>
  412. ```
  413. The advantage with this method is that editing is done in the config.xml file which will most often be in your source control anyway and hence, changes to it will be reserved.
  414. ## 5. Credits ##
  415. This plugin was enhanced for Plugman / PhoneGap Build by [Eddy Verbruggen](http://www.x-services.nl).
  416. The Android and Windows Phone code was entirely created by the author.
  417. The first iteration of the iOS code was inspired by [Cameron Lerch](https://github.com/bfcam/phonegap-ios-social-plugin).