Uw betrouwbare partner voor (mobiele) web applicaties
TwitterEmailRSS

PhoneGap share plugin for Facebook, Twitter and other social media

PhoneGap share plugin

Adding social media to your PhoneGap app is always a good idea – you can draw attention to your app by letting your users do the promotional work for you. All you need to do is adding share buttons for Twitter, Facebook and other social media to your app. BUT how does this work on PhoneGap.. and what about PhoneGap Build? Use this PhoneGap share plugin and you get stuff like this:

PhoneGap share plugin in action on iOS7

SocialSharing plugin in action on iOS 7

PhoneGap share plugin in action on Android

SocialSharing plugin in action on Android 4.4 (Kitkat)

Share text using the native sharing widget

Using this plugin, you can have your iOS or Android device open the native share widget, so the user can choose where to share the content to. For example, sharing a message ‘My message’ can be started by clicking a button:

<button onclick="window.plugins.socialsharing.share('My message')">share!</button>

Sharing a link

Sharing text is nice, but why not spice it up a little? Let’s add a link:

  share('My message',
        null,
        null,
        'http://www.x-services.nl')

Sharing an image

What about sharing an image? That’s possible as well. You can share links from the internet and from the local device. You can even share base64 encoded image which you just grabbed from the Camera!

  share('My message',
        'PhoneGap share plugin',
        'https://www.google.nl/images/srpr/logo4w.png', // check the repo for other usages
        'http://www.x-services.nl')

Sharing text, a subject, a link and an image together

Let’s combine all options and share like a boss! A subject can be used for the email client, but other apps may use it as well, but that’s up to the receiving app:

  share('My message',
        'PhoneGap share plugin',
        'https://www.google.nl/images/srpr/logo4w.png',
        'http://www.x-services.nl')

Skipping the share dialogue

If you don’t want your user to choose where to share to, you can skip the share widget and directly share to Twitter, Facebook, or other apps:

  shareViaTwitter('My message');

  shareViaFacebook('Message via Facebook',
                   null,
                   null,
                   console.log('share ok'), // success callback
                   function(errormsg){alert(errormsg)}) // error callback

  shareVia('com.apple.social.sinaweibo' // for options, see the repo
           'Message via Weibo on iOS',
           null,
           null,
           console.log('share ok'), // success callback
           function(errormsg){alert(errormsg)}) // error callback

What about PhoneGap Build?

Check out the plugin page and search for the SocialSharing plugin (it’s one of the most used plugin thanks to you all!).

66 thoughts on “PhoneGap share plugin for Facebook, Twitter and other social media

  1. Hi Albert!

    Thanks for your question. Let’s figure out what the problem is you’re experiencing and I’ll add the solution to the README of the Github repo. Can you please send me a private mail at eddyverbruggen[at]gmail.com and attach your non working Phonegap local project? Either Android or iOS will do just fine. If mailing it doesn’t work, consider a 3rd party solution like wetransfer.

    And yes, the plugin is compatible with all 3.x versions of PhoneGap (and 2.x as well, but it’s in a separate branch in Github and doesn’t support all features of the 3.x version).

    Best,
    Eddy

  2. Hi Eddy

    I’m implementing your plugin on my phonegap app with great results.
    Is there a way of sharing the contents of the page, for emailing or printing?

    Thanks!

    • Hi,

      I think you have to make a screenshot of your page and share that via the plugin as an image. Try Googling for phonegap screenshot plugin, or something similar to save the contents of a page. Please post back the results because it sounds like an interesting usecase!

      Thanks,
      Eddy

  3. Eddy

    I was thinking of using a variable to take the div’s contents than passing that variable to your plugin for printing, could it be possible or is it just my lunatic idea?

    Thanks,
    Lucas

    • Yes, content of a div can be shared, but your result may vary (not sure about formatting and images etc inside the div). You can pass the content as text, but that will be hard to share to twitter for example. You’ll have to experiment a bit. Let me know the results 🙂

  4. Hi.
    I am Phonegap, Icenium Developer, I have trouble to add this social sharing plugin as custom plugin on Icenium project.

    I can’t see the new sharing widget when I call window.plugins.socialSharing.share(‘Message and subject’, ‘The subject’); and others also.
    It works fine with eclipse project, but not working with Icenium custom plugin.
    Best regards.
    Cui

    • Hi Cui,

      I recently added the plugin to Icenium Mist myself and the only difference I can see is that you use socialSharing.share, but it should be socialsharing.share (all lowercase).

      Can you let me know what happens after changing that line? Otherwise, I can send you a little document on how to integrate the plugin in Icenium Mist (if that is what you are using).

      Greets,
      Eddy

  5. Hi Eddy

    I’m calling the variable included in my js file:
    var testPage = $( "#myDiv" ).text();

    Then I’m passing the variable into the share plugin:
    window.plugins.socialsharing.share(testPage)

    Sure it’s not working yet, the shared email shows “testPage” and not the contents of the div. Any ideas?

    • That is weird. I just tried this and it correctly shares the content of my div (sorry for the crappy code, need to check the parser on the site):

      < div id="myDiv">some nice text here< /div>
      < button onclick="var testPage = $('#myDiv').text(); window.plugins.socialsharing.share(testPage)">share text from that div< /button >

      Are you sure you don’t have quotes around the testPage param in the share() function?
      If testing my code doesn’t work, can you share your full code with me (via email)?

  6. Hi Eddy,

    I love your plugin. It’s very usefull, thank you for this.

    Is it possible to put google plus in the iOS sharing box?

    Thanks

    • Hmm, not really, because Apple is kinda in control of what is in that box. It may be possible to add a shareViaGooglePlug function, but it’s not planned at the moment.

  7. Eddy

    You’re right it works perfectly. Maybe the trick is to pass the variable inside the button routine.
    This way we can print or email the div contents, now that’s amazing or what?

    Thanks so much,
    Lucas

  8. Hi,
    I followed your intructions step by step.
    But when i click on the button.. it shows an error message – “exec() Call to unknown plugin SocialSharing”.
    I am using cordova 2.6.0.

    Kindly help!!

  9. I implemented this. I’m using cordova (phonegap) 3.0.

    It works good in iOS6.

    In iOS7 the sharing view is not showing the Facebook and twitter icon. If I try to share directly with Facebook, for example, msg via Facebook (with errcallback) I get back the message error that “is not available”. I’m guessing from this part of the code of the socialsharing.m class:
    (void)share:(CDVInvokedUrlCommand*)command {

    if (!NSClassFromString(@”UIActivityViewController”)) {
    CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@”not available”];

    Has anyone experience this issue with iOS7 implementation of this component and the Facebook and twitter sharing functionality?

  10. Hello Eddy,

    Yes I used the 2.x branch of the plugin. But on click of the button now it shows an error message – ” Uncaught TypeError: Cannot read property ‘socialsharing’ of undefined at file://android_asset/www/index.html :13 “.
    I have emailed you my TestApp from my hotmail email id and the subject of the email is – “TestApp – Manu”.
    Please look into it and help me out.

    Thanks.

  11. Hi Alan,

    I’d like to investigate this issue, so yes, can you please share your code with me? You can mail it to eddyverbruggen[at]gmail.com. If you’re building locally (not build.phonegap.com), then please zip up the entire XCode project.

    Thanks!
    Eddy

  12. Hi,

    I follow your instruction carefully, but when I click on the button it alert “CREATE_DIR_FAILED”.
    I am sure that I added permission “WRITE_EXTERNAL_STORAGE” already in manifest file. Please help me.

    • Hi, can you please send me your Android project via email (eddyverbruggen[at]gmail.com), dropbox, or wetransfer?

    • I am also got the same error. It always result the error : CREATE_DIR_FAILED. Unable to create external files directory. What i will do for solving this issue? Please reply me.

      Any help would be appreciated !

  13. Hello! Any way to include HTML text parsed in body of share? When sharing, we need to inlcude an html format in the body of either email or other shares…ideas?

    • Hi,

      I don’t think it’s allowed to share html code to twitter/facebook and the likes. To mail it should be possible, but you may be better off using an email-only plugin for that..
      I don’t really have the time to investigate html sharing, but I’m curious what you can find out using this plugin or a different one.

      G’luck,
      Eddy

  14. Hello!

    First off, I absolutely love this plugin. It works extremely well and was super simple to set up. I am having an issue and I’m hoping you can help me.

    My Phonegap 3.3 project runs on both iOS and Android. Your plugin works perfectly on iOS 6, iOS 7 and Android phones running Android 4.4

    I’m attempting to run on a Motorola Droid Incredible running Android 4.1.2 and the social plugin isn’t working. I make a call to it from various different methods, and nothing shows on the screen. Any ideas?

    • Hi, thanks for the kind words!
      I wish I had such a device to test it. My own 4.1 device works find by the way.
      Have you tried connecting it to your computer and run ‘adb logcat’ while clicking the share button? Maybe you’ll see an error which can help us tackling this issue.

      • Thankfully, you were right about logcat giving me some clues! it was failing when trying to create the temp directory upon calling the plugin. I commented out the line that called create and the plugin is now working for me on every device.

        final String dir = webView.getContext().getExternalFilesDir(null) + “/socialsharing-downloads”;
        // createDir(dir);

        I *DO* call permissions to WRITE_EXTERNAL_STORAGE in my manifest, so I don’t know what this could be. I know that my Droid Incredible does NOT have an SD card in it.

        Hopefully this helps you out. Let me know what you think. If you need me to run anything on any of my devices, don’t hesitate to contact me. Shoot me an email!

        • I had the exact same “issue” on a Motorola Droid 4 running under Android 4.1.2.

          The problem is that webView.getContext().getExternalFilesDir(null) apparently returns “null” if the external storage is mounted somewhere else (typically, when connected to a computer while you’re trying to debug this issue).

          This is perhaps a “normal” way to handle it, as stated by the Android documentation : External files are not always available: they will disappear if the user mounts the external storage on a computer or removes it. See the APIs on Environment for information in the storage state..

          So a simple workaround is simply to not mount the storage when testing sharing functionality.

  15. Hi, yes you can! Just setup your facebook/twitter accounts as usual in the Settings of your simulator and share as you like.

  16. Hello. Since your plugin tweets already (if you are authorized already with twitter application), can you go about retweeting or replying? meaning: When I share, can I pass something in the body or parameters to rewteet or reply?

    A reply basically is the @user and a message but something gets flagged.
    A retweet you could add RT but is there a way since you are authorized to retweet?

    Thanks for any input…

  17. Hi

    This is an excellent plugin for social sharing in phonegap. I have successfully implemented facebook and twitter sharing using this plugin,BUT I am not finding a way to implement goolge-PLUS sharing using this plugin. How do I implement google plus sharing using this plugin?

  18. Greetings and many thanks for such a useful plugin to the community 🙂
    I am having some trouble and could use some help.
    I am using phonegap 3.1.0 (through phonegap build service) and your plugin version 4.0.1
    I am trying to implement a button with all arguments. It works just fine if i choose e.g. gmail from the app list, but when i choose facebook only the image gets transferred. I have tried this on Blue Stacks emulator and on my phone running Android 4.1.2 with similar results.
    Any ideas as to why this is happening and -most importantly- how it can be fixed?

  19. Many many thanks for your plug-in!!! I can share the content to whatsapp easily now!!
    Btw, could you consider add share to “Line” ( http://line.me/en/ ) on next version?

    This is because I can share the msg to Line in IOS is easily, just use line://xxx , however, in android is hard to do that, thank you!

    (Sorry for my bad english)

  20. Hi Eddy, I´m developing a Phonegap app and I think your plugin can be very useful for me. But I´d like to know, if it´s possible to filter specific apps (both Android and iOS), for example, if I only want to share with Facebook, Twitter, Whatsapp, Gmail and native mail app.

    Thanks.

  21. Hi, nice plugin, congratulations. I have installed it on cordova 3 and its working fine. But I need to share to the printer app. Can you please give me a hint on what files should I edit (js, html) and what code to add to make it share a ? I have tried the code from Kinglybird, but no luck so far.
    Best regards,
    BK.

  22. Hello,

    Thanks for this plugin…
    Is this possible to give a like button for facebbok page like.
    If Facebook app is installed on a device then check authentication from it or if it is not then open facebook browser for same.
    Please let me know…

  23. Hello Eddy.
    Is it possible to include WhatsApp in the UIActivityViewController?

    Thanks
    Fran

  24. Hi Eddy.

    I have used you plugin for phonegap and it is working perfectly for me. But it seems that I could not set location to my post for Facebook or Twitter. So is this feature to set location for share/post possible?

    Thanks!
    Sean

  25. Hi Alvaro,

    The alert you see is probably a copy-paste of the sample code of the repo. You can replace the alert() by a console.log(), or if you really want to show feedback to the user after the plugin returns, you can use a native navigator.notification.alert (https://github.com/apache/cordova-plugin-dialogs/blob/dev/doc/index.md).

    It’s even cooler to show a Toast (a little native feedback message which automatically disappears after a few seconds). You can use this plugin in place of the alert: https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin

    Hope this clears things up 🙂
    Eddy

  26. Hello Eddy

    Love the Plugin working great on both Android & ios for Facebook & Twitter! I had a Request to add some other social media apss to the mix, specficaly Pintrest & Tumblr. Can you point me in the right diretion & let me know if it’s possible to add those social media networks to this plugin?

    Thanks!

  27. Hi,
    How can i use the locally saved image url (Pictures in Gallery folder) path, instead of the http remote url to share to facebook and twitter.

    • Hi, if the app can access the file and you know its name, you can try:

      window.plugins.socialsharing.share(‘my msg’, ‘my title’, ‘file:///thepath/filename.jpg’);

      For more examples see the readme in the repo, right after this line: “Want to share images from a local folder (like an image you just selected from the CameraRoll)?”

  28. Hey, its working great for me.

    But how can i Use the email function ?

    window.plugins.socialsharing.shareViaMail(….

    does not work. -.-

  29. Hi Eddy,
    Are there any plans to support sharing via facebook/twitter (developer) apps? In other words, passing an app ID and prompting the user to grant permissions? I’m guessing you probably don’t plan to support this, and that it would be marvelously cumbersome for you to try. Given that, do you know of any other plugins that do support this? I’ve searched and searched but the closest I found was: https://github.com/phonegap/phonegap-facebook-plugin
    Thanks!

    • Hi Jason, you’re right, I won’t be adding deeper integration, that’s what the Facebook Connect plugin is for. I haven’t tried any others but I will soon.

  30. Hello!
    I love this plugin! is easy and useful.
    I want to give an idea for making it work (in some way) in FB for Android:
    If you call the plugin like that: window.plugins.socialsharing.share(‘http://www.google.com’), it lets you share a URL, and so you have something to share in facebook.
    (If it does not work too, do this: window.plugins.socialsharing.share(‘http://www.google.com’, null, null, ‘string’))
    Thanks again for the plugin!!

  31. Hi Eddy, I’m using phonegap build and using this social sharing plugins. But I can’t seem to make it work. I’ve tried only putting 1 single button(I copied from the example) in a single page just to try this plugin, and it still doesn’t work.
    I tapped the button, nothing happened.. no share popup

    • Hi, I’m just letting you know, I’ve solved my problem.
      I studied your example app.
      To make this plugin worked, I need to put and I use your social-sharing-demo.js

      Thanks for this great plugin 😀

  32. Great work on this plugin. I have a question though, on Android when I share with WhatsApp but then decide not to share with a user, then I still get a success response. So that’s the opposite of the Facebook issue that is already mentioned. Is this normal?

  33. Hi,

    I’ve added the plugin to my PhoneGap Build project and added ‘window.plugins.socialsharing.share(‘Message and subject’, ‘The subject’);’ to a button to activate the sharing options however when deployed on an iOS phone it’s not showing the share options – it simply doesn’t seem to do anything and you can continue to use the app as normal.

    Entering the debug mode and checking the console reveals no error messages. Putting a console.log() before the socialsharing call shows the buttons code is being called and the console.log() is reached however putting the console.log() after the socialsharing call doesn’t return anything – almost as if it’s hit an error and not continued.

    Not too sure how to debug this further! Any help is much appreciated.

  34. I am using this plugin for sharing on twitter and mail. But with Facebook I am not able to share any text or description. Also I have a Facebook app. But I can’t find any documentation which states about this type of sharing. Thanks in advance

    • It’s in the readme: sharing text to fb on Android is just not possible due to a restriction of the fb app itself.

  35. Hi,

    Im having an issue with the plugin, im using Cordova 3.5.0 with phonegap build and i am trying to tweet/share an image.

    Sharing via text works great but when i tap the FB/Twitter options nothing happens, the app is unresponsive for a few seconds and the neither the success or error function are called

    Example:

    In my app i have a button and an image like so:

    Share

    I try to share this with the world like so:

    shareSuccess = function(){ alert(‘success’); }
    shareError = function(){ alert(‘error’); }

    share = function(){
    var img = document.getElementById(‘felix’).src;
    window.plugins.socialsharing.share(‘My Pic’, ‘Check out my pic’, img, null, shareSuccess, shareError);

    };

    document.getElementById(‘share-btn’).addEventListener(‘touchstart’, share, false);

    Any ideas?

    Thanks in advance,
    Si

  36. I’m using this plugin and it works great for iOS, but when I publish to android I get:

    java.lang.SecurityException: Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL

    Despite setting this permission in AndroidMainfest.xml.. any ideas? Have you seen this before?

    Thanks

  37. Is it possible to share pdf anf xml file by using socialSharing plugin in cordova for windows phone 8?

Comments are closed.