Share something to a phonegap app

11,094

Solution 1

To appear in this list you have to modify the AndroidManifest.xml file and add the following lines under your activity :

<intent-filter> 
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />
</intent-filter>

This will make your app appear in the list. Now I think you may probably also want to know how to handle this in your code. When another application is going to share some text with you, it's going to start your application with an Android "Intent". To be able to use Intents, you need a PhoneGap plugin. I think WebIntent might suit you. This would be the code :

// deviceready is PhoneGap's init event
document.addEventListener('deviceready', function () {
  window.plugins.webintent.getExtra(WebIntent.EXTRA\_TEXT, function (url) {
    // url is the value of EXTRA_TEXT 
  }, function() {
    // There was no extra supplied.
  });
});

More info on WebIntent here : http://smus.com/android-phonegap-plugins/

Note : I don't think you'll be able to do this with PhoneGap Build though ... you can only use supported plugins, and can't change the AndroidManifest.xml file that much. You'll probably have to go the Cordova route and build everything on your machine.


Edit : there are a few people asking how to do this on iOS. There are two steps to do this :

  1. Associate your app with the right file type associations by adding the relevant information in your info.plist. This SO answer explains how to do it : How do I associate file types with an iPhone application?. This will make your app appear in the list but your app won't receive the data yet.
  2. Your application will now be launched with new parameters. You now have to be able to read these parameters. Check this question/answer on SO, it does exactly that : How to pass arguments to app built on Phonegap

Solution 2

I managed to get the URL of file to be shared; by using EXTRA_STREAM insted of EXTRA_TEXT inside deviceready, modifying Webintent.java, and adding an intent filter in AndroidMenifest.xml.

This is how my main activity looks:

    <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize">
        <intent-filter android:label="@string/launcher_name">
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter android:label="@string/app_name">
            <data android:mimeType="*/*" />
            <action android:name="android.intent.action.SEND" />                
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

I found that the plugin had a bug for EXTRA_STREAM part, so i modified my javafile according to this:

https://github.com/Initsogar/cordova-webintent/issues/23

Also inside my js file, I have to call the function like this:(use of EXTRA_STREAM instead of EXTRA_TEXT )

    window.plugins.webintent.getExtra(window.plugins.webintent.EXTRA_STREAM, function (url) {
        // url is the value of EXTRA_STREAM 
        alert(url);
      }, function() {
        // There was no extra supplied.
        alert("no url");
      });

and I get the URI of the file.

Solution 3

@ericpeters0n - When using iPhone apps and Sharing, where does this list of apps come from then? For example, when I'm in the Photo Gallery and I share, the list includes Message, Mail, Twitter, Facebook, Flickr. How did these apps register themselves to appear on this list?

Share:
11,094

Related videos on Youtube

karacas
Author by

karacas

https://github.com/karacas https://twitter.com/krc_ale

Updated on June 06, 2022

Comments

  • karacas
    karacas almost 2 years

    Is there any way to register a phonegap app to appear in the menu of applications to share?

    enter image description here

  • ericpeters0n
    ericpeters0n almost 10 years
    It appears this answer was copied from the aged webIntent plugin website... Is there more to this? I'm not receiving EXTRA_TEXT or URI via WebIntent on deviceready or resume.
  • Sébastien Nussbaumer
    Sébastien Nussbaumer almost 10 years
    @ericpeters0n are you sure the plugin is correctly installed ? I've never done what is asked here with PhoneGap but with other technologies, and know for sure that if the intent filter is written as above as a child of your activity, then you activity WILL appear in the list and will receive the intent. If you don't get it with WebIntent that means that either WebIntent is not correctly installed/running, either WebIntent does not work anymore with recent versions of Android (Kitkat ?), but I have some doubt about the latter ... the principle of intents has not changed for quite a while...
  • Marius George
    Marius George almost 10 years
    How would this work on iPhone? The above example is for Android only, and I'm interested to know how an app can be similarly registered on iPhone for sharing via the app.
  • ericpeters0n
    ericpeters0n over 9 years
    @SébastienNussbaumer - Thanks for the follow up; It was a manifest issue as it worked out.
  • ericpeters0n
    ericpeters0n over 9 years
    @MariusGeorge - Prior to the new version (8), iOS' architecture would not support such behavior. Now we're just awaiting a Cordova plugin to wrap the analagous iOS behavior.
  • Eno
    Eno over 9 years
    Versions of iOS prior to 8 had to have this functionality kinda hard-wired into them. In contrast, Android apps simply register to receive the sharing intent and automatically get added to the list (much smarter design IMHO :-)
  • ryanyuyu
    ryanyuyu almost 9 years
    If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context.
  • Moinkhan
    Moinkhan almost 9 years
    I thought I typed and answer..though i'm sorry if it was too confusing to look like an answer (tried to simplyfy it chek the edit)
  • Hitu Bansal
    Hitu Bansal almost 9 years
    @ericpeters0n: Can we do in ios8 now ? Any idea pls ?
  • Hitu Bansal
    Hitu Bansal almost 9 years
    @SébastienNussbaumer: i already tried this. but not sucessfull.. I have posted a new question as well stackoverflow.com/questions/30725432/…