Search code examples
androidioscordovawebviewinappbrowser

Enable download in inAppBrowser - Phonegap / Cordova


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...


Solution

  • 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