3

I'm using cordova 3.5-0.26 & inAppBrowser 0.5.0

Now I'm loading an external page in inAppBrowser. There is a download button. When I press the download it doesn't do anything. I thing download is off in inAppBrowser. So as in the cordova view.

Then I tried to active the download manager using following code (for Android)

appView.setDownloadListener(new DownloadListener() {
                    public void onDownloadStart(String url, String userAgent,
                            String contentDisposition, String mimetype,
                            long contentLength) {
                      Intent i = new Intent(Intent.ACTION_VIEW);
                      i.setData(Uri.parse(url));
                      startActivity(i);
                    }
                });

This doesn't work for the inAppBrowser either.

1) How can I enable the download in inAppBrowser? 2) Is there any way to catch the download with default download manager? 3) If possible please mention solution for both ios and android. if not android will do for now. Please help...

AtanuCSE
  • 8,832
  • 14
  • 74
  • 112
  • 1
    You will need to open the stock browser if you want to use Android's download manager. Unless you are ready to tweak Cordova's Java file to catch any downloadable files and execute it via AsyncTask. More reading here http://stackoverflow.com/a/12078937/541591 – James Wong Aug 13 '14 at 05:05
  • If no other way shows up, I'm ready to tweak the java files. I'll tweak the inAppBrowser java file to force it to download. Though I'm expecting a better solution for both iOS & android. I don't know objective C. @JamesWong – AtanuCSE Aug 13 '14 at 05:46
  • And I've no problem of using built-in browser's download manager. – AtanuCSE Aug 13 '14 at 05:47
  • Alternatively you can use Cordova's File API to download the file, your code will work cross platform then. – James Wong Aug 13 '14 at 09:10
  • nope. That's not an option. External link is a third party website. I have no control over it. So won't have the download url. @JamesWong – AtanuCSE Aug 13 '14 at 09:15

1 Answers1

-1

InAppBrowser doesn't allow download. You will need to modify plugin to allow it for downloading.

For android, inside platforms\android\src\org\apache\cordova\inappbrowser

method name private void navigate(String url) {

include

this.inAppWebView.setDownloadListener(new DownloadListener() {
                    public void onDownloadStart(String url, String userAgent,
                            String contentDisposition, String mimetype,
                            long contentLength) {
                      Intent i = new Intent(Intent.ACTION_VIEW);
                      i.setData(Uri.parse(url));
                      cordova.getActivity().startActivity(i);
                    }
                });

before this line this.inAppWebView.requestFocus();

again same code in the method public void run() {

after this segment

if (clearAllCache) {
                CookieManager.getInstance().removeAllCookie();
            } else if (clearSessionCache) {
                CookieManager.getInstance().removeSessionCookie();
            }

            inAppWebView.loadUrl(url);

in your .java file inside onCreate

appView.setDownloadListener(new DownloadListener() {
                    public void onDownloadStart(String url, String userAgent,
                            String contentDisposition, String mimetype,
                            long contentLength) {
                      Intent i = new Intent(Intent.ACTION_VIEW);
                      i.setData(Uri.parse(url));
                      startActivity(i);
                    }
                });

Don't know about iOS

AtanuCSE
  • 8,832
  • 14
  • 74
  • 112