errorous
errorous

Reputation: 1071

Chrome WebView: triggering native Android Share function

Is it possible, by any chance to invoke Android's native share(file) functionality within Chrome WebView? I'm creating a hybrid Ionic AngularJS app, and would like to use the Android's native share functionality for files. I've tried different plugins, but all of them use custom share implementation for each of the apps present on the device, which does not suffice.

Upvotes: 2

Views: 1799

Answers (1)

Steven
Steven

Reputation: 1414

Did you tried it to making an JavascriptInterface

Add this in your MainActivity.java

public class WebViewJavaScriptInterface {
        private Context context;

        @JavascriptInterface
        public void sharingIsCaring(String code) {
            openSharing(code);
        }
}

This is the action that starts the sharing:

public void openSharing(String code) {
        String message = "Your share message: " + code;
        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("text/plain");
        share.putExtra(Intent.EXTRA_TEXT, message);
        startActivity(Intent.createChooser(share, "Share your code"));
    }

So, how do you trigger the @JavascriptInterface

You need to add this in your OnCreate() method:

view.addJavascriptInterface(new WebViewJavaScriptInterface(this), "SmashIt");

So to trigger it from the JS you only need to call the function sharingIsCaring()

You need to do that by placing SmashIt for the function that you call so Android will know it's him you are calling. So it will look like this:

SmashIt.sharingIsCaring(code)

Upvotes: 2

Related Questions