Enable download in inAppBrowser - Phonegap / Cordova

给你一囗甜甜゛ 提交于 2019-12-02 05:28:00

问题


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


回答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



来源:https://stackoverflow.com/questions/25278373/enable-download-in-inappbrowser-phonegap-cordova

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!