Skip to content

[webview_flutter] Add download listener on Android webview #89657

@renefloor

Description

@renefloor

Use case

In the Flutter webview we have a navigationDelegate so we can do specific actions in the app based on the url that is being loaded.

In the webview we have a button that downloads a text file. On iOS this file is received in the navigationDelegate as data:text/plain;charset=utf-8,{text}, but on Android nothing happens. So there my research started.

On Android the WebViewClient.shouldOverrideUrlLoading is being overriden and on iOS it is WKNavigationDelegate.decidePolicyForNavigationAction.
According to this StackOverflow answer the decidePolicyForNavigationAction is also used to start a download. However, on Android this is done in setDownloadListener, for example:

        webview.setDownloadListener(DownloadListener { url, userAgent, contentDisposition, mimetype, contentLength ->
            val i = Intent(Intent.ACTION_VIEW)
            i.data = Uri.parse(url)
            startActivity(i)
        })

Proposal

My proposal is to add setDownloadListener to the webview and send this url to the navigation delegate. We might want to send more information, but this way the behaviour of Android and iOS are exactly the same.

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Flutterp: webviewThe WebView pluginpackageflutter/packages repository. See also p: labels.waiting for PR to land (fixed)A fix is in flight

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions