cordova-plugin-x-socialsharing

repository·master·Indexed 23 days ago

https://github.com/eddyverbruggen/socialsharing-phonegap-plugin

A PhoneGap/Cordova plugin that enables mobile applications to use the native device sharing functionality to share text, links, images, and files to various social media apps and other installed applications. It supports Android, iOS 6 and up, and WP8. Key features include the shareWithOptions method for cross-platform compatibility, direct sharing to apps like Twitter, Facebook, Instagram, and WhatsApp, as well as sharing via SMS and Email.

Tokens
3.5K
Snippets
8
Records
19
Agent score
81%

What's inside cordova-plugin-x-socialsharing

  1. Plugin Overview and Capabilities

    master

    The SocialSharing plugin allows you to trigger the native sharing window of a mobile device.

    Key Features:

    • Share Content: Share text, links, images, or other files (e.g., PDF, ICS).
    • Subject Support: Supports a 'subject' field if the receiving application supports it.
    • File Sources: Supports sharing files from the internet, the local filesystem, or the www folder.
    • Direct Sharing: Allows skipping the general share dialog to share directly to specific apps like Twitter or Facebook.
    • Compatibility: Compatible with Cordova Plugman and officially supported by PhoneGap Build.

    Version Note: Version 6.0.0+ is compatible with Android X. If you encounter Android X issues, use version 5.6.8.

  2. Use social sharing on Windows Phone (WP8)

    master

    On Windows Phone 8, the plugin provides available, canShareViaEmail, share, shareViaEmail, and shareViaSMS.

    Important: When using the share method on WP8, you must pass null for any unused attributes to ensure correct behavior.

    Supported sharing flavors:

    • Message only: Pass the message and null for other fields.
    • Message, title, and link: Pass the message, title, null for image, and the URL.
    • Image only: Pass the message, title, an image URL, and null for the link. Note that only images from the internet are supported, and if an array is passed, only the first image is used.
  3. Manually install the SocialSharing plugin

    master

    If you cannot use a CLI, follow these steps to manually integrate the plugin:

    1. Configure config.xml

    Add the appropriate feature tag to your config.xml files:

    iOS:

    <feature name="SocialSharing">
      <param name="ios-package" value="SocialSharing" />
    </feature>

    Android:

    <feature name="SocialSharing">
      <param name="android-package" value="nl.xservices.plugins.SocialSharing" />
    </feature>

    Windows Phone:

    <feature name="SocialSharing">
      <param name="wp-package" value="SocialSharing"/>
    </feature>

    2. Platform Specific Requirements

    Android: To share remote files (like images) on Android, you must first store them locally. Add this permission to your AndroidManifest.xml:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    iOS: You must add Social.framework and MessageUI.framework to your project. In Xcode, go to Build Phases > Link Binary With Libraries and add both frameworks.

    3. Add JavaScript and Source Files

    1. Copy SocialSharing.js to your project and reference it in index.html:
    <script type="text/javascript" src="js/SocialSharing.js"></script>
    1. Copy the platform-specific source files to the following locations:
    • iOS: Copy SocialSharing.h and SocialSharing.m to platforms/ios/<ProjectName>/Plugins
    • Android: Copy SocialSharing.java to platforms/android/src/nl/xservices/plugins (create the folders if they don't exist)
    • Windows Phone: Copy SocialSharing.cs to platforms/wp8/Plugins/nl.x-services.plugins.socialsharing (create the folders if they don't exist)
    <!-- for iOS -->
    <feature name="SocialSharing">
      <param name="ios-package" value="SocialSharing" />
    </feature>
    
    <!-- for Android (you will find one in res/xml) -->
    <feature name="SocialSharing">
      <param name="android-package" value="nl.xservices.plugins.SocialSharing" />
    </feature>
    
    <!-- for Windows Phone -->
    <feature name="SocialSharing">
      <param name="wp-package" value="SocialSharing"/>
    </feature>
  4. Install the SocialSharing plugin via PhoneGap Build

    master

    To use the plugin with PhoneGap Build, add the following XML to your config.xml.

    To always use the latest version published to plugins.cordova.io:

    <gap:plugin name="cordova-plugin-x-socialsharing" source="npm" />

    To use a specific older version hosted at PhoneGap Build:

    <gap:plugin name="nl.x-services.plugins.socialsharing" version="4.3.16" />

    Note: Ensure you include a reference to cordova.js in your index.html head:

    <script type="text/javascript" src="cordova.js"></script>
  5. Exclude activities from the iOS share sheet

    master

    You can customize the iOS share sheet by excluding specific activities. Add the SocialSharingExcludeActivities key to your app's main .plist file with an array of activity strings.

    Available activities to disable:

    • com.apple.UIKit.activity.PostToFacebook
    • com.apple.UIKit.activity.PostToTwitter
    • com.apple.UIKit.activity.PostToFlickr
    • com.apple.UIKit.activity.PostToWeibo
    • com.apple.UIKit.activity.PostToVimeo
    • com.apple.UIKit.activity.TencentWeibo
    • com.apple.UIKit.activity.Message
    • com.apple.UIKit.activity.Mail
    • com.apple.UIKit.activity.Print
    • com.apple.UIKit.activity.CopyToPasteboard
    • com.apple.UIKit.activity.AssignToContact
    • com.apple.UIKit.activity.SaveToCameraRoll
    • com.apple.UIKit.activity.AddToReadingList
    • com.apple.UIKit.activity.AirDrop
    <key>SocialSharingExcludeActivities</key>
    <array>
      <string>com.apple.UIKit.activity.AssignToContact</string>
      <string>com.apple.UIKit.activity.CopyToPasteboard</string>
    </array>
  6. Use the Web Share API

    master

    For environments supporting the standard Web Share API (like Chrome), you can use navigator.share to share data. This method is spec-compliant but may offer fewer options than the plugin's native methods. It accepts an object containing title, text, and url.

    navigator.share({
      'title': 'Optional title',
      'text': 'Optional message',
      'url': 'http://www.myurl.com'
    }).then(function() {
      console.log('Successful share');
    }).catch(function(error) {
      console.log('Error sharing:', error)
    });
  7. Share directly to specific social apps

    master

    You can bypass the general share sheet and target specific applications directly using specialized methods.

    Twitter

    Use shareViaTwitter(message, [img], [url], [success], [error]).

    Facebook

    Use shareViaFacebook(message, [img], [url], [success], [error]). Workaround for Facebook prefilled message bugs: Use shareViaFacebookWithPasteMessageHint.

    • Android: Shows a Toast message prompting the user to paste.
    • iOS: Shows a short message prompting the user to paste (since v4.3.18).
    • iOS Requirement: You must whitelist fb:// in your .plist file under LSApplicationQueriesSchemes.

    Instagram

    Use shareViaInstagram(message, [img], [success], [error]).

    • iOS Requirement: Use canShareVia('instagram') first and whitelist the URL scheme.

    WhatsApp

    Use shareViaWhatsApp(message, [img], [url], [success], [error]).

    • Direct to contact (Android): Use shareViaWhatsAppToPhone(phoneNumber, message, [img], [url], [success]).
    • Direct to contact (iOS/Android): Use shareViaWhatsAppToReceiver(addressbookIdOrPhoneNumber, message, [img], [url], [success]). On iOS, use the Addressbook ID from the Cordova Contacts Plugin.
  8. Share via SMS or Email

    master

    SMS

    Use shareViaSMS(message, [phoneNumbers], [success], [error]).

    • To prefill multiple numbers on Android 4.4+, pass a comma-separated string of numbers.
    • To include a subject or image (iOS only), pass an object as the first argument: {message: '...', subject: '...', image: '...'}.

    Email

    Use shareViaEmail(message, subject, [to], [cc], [bcc], [files], [success], [error]).

    • to, cc, and bcc must be null or an array of email strings.
    • files can be null, a single string, or an array of strings.
  9. Use shareWithOptions for cross-platform sharing

    master

    The shareWithOptions method is the recommended way to share content as it is the most feature-rich and provides cross-platform compatibility. It allows you to pass a single options object containing all shareable content. It also provides feedback via a success callback, indicating if the share was completed and which app was used (though Android app reporting varies by version).

    Options Object Schema:

    • message: String. The text to share. (Note: Not supported by some apps like Facebook or Instagram).
    • subject: String. The subject line (e.g., for email).
    • files: Array of strings. Filenames or URLs (local or remote).
    • url: String. A web link to share.
    • chooserTitle: String (Android only). Overrides the default share sheet title.
    • appPackageName: String (Android only). The ID of a specific app to share with.
    • iPadCoordinates: String (iOS only). Format: x,y,width,height to define the popover location.
    var options = {
      message: 'share this',
      subject: 'the subject',
      files: ['', ''],
      url: 'https://www.website.com/foo/#bar?a=b',
      chooserTitle: 'Pick an app',
      appPackageName: 'com.apple.social.facebook',
      iPadCoordinates: '0,0,0,0'
    };
    
    var onSuccess = function(result) {
      console.log("Share completed? " + result.completed);
      console.log("Shared to app: " + result.app);
    };
    
    var onError = function(msg) {
      console.log("Sharing failed with message: " + msg);
    };
    
    window.plugins.socialsharing.shareWithOptions(options, onSuccess, onError);
  10. Check if an app is available with canShareVia

    master

    Before attempting to share to a specific app, use canShareVia to verify availability. This prevents error callbacks from being triggered unnecessarily.

    Parameters: canShareVia(appIdentifier, [message], [img], [url], [success], [error])

    • Android: The errorCallback returns a JSON array of available package names if the requested one is not found.
    • iOS: You can use identifiers like com.apple.social.facebook or whatsapp (requires whitelisting whatsapp:// in .plist).
    • Custom Activities (Android): You can specify a specific activity using packageName/activityName (e.g., com.tencent.mm/com.tencent.mm.ui.tools.ShareImgUI).