18

I have a simple android WebView application which is displaying a website featuring 'copy' buttons using the following code:-

navigator.clipboard.writeText('Text to be copied')
  .then(() => {
    console.log('Text copied to clipboard');
  })
  .catch(err => {
    // This can happen if the user denies clipboard permissions:
    console.error('Could not copy text: ', err);
  });

this is working in all desktop and mobile browsers and an iOS WebView application however I cant it fails and catches the following error within a android WebView application: DOMException: write permission denied.

these are the settings for the WebView within the app:

    webView.settings.javaScriptEnabled = true
    webView.settings.userAgentString = "EngageMobileApp Android"
    webView.settings.builtInZoomControls = true
    webView.settings.displayZoomControls = false
    webView.settings.allowFileAccess = true
    webView.settings.setAppCacheEnabled(true)
    webView.settings.domStorageEnabled = true
    webView.settings.loadWithOverviewMode = true
    webView.settings.allowContentAccess = true
    webView.settings.mediaPlaybackRequiresUserGesture = false
    webView.settings.javaScriptCanOpenWindowsAutomatically = true
    WebView.setWebContentsDebuggingEnabled(true)

and the set permissions within the manifest:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
luke-tolley
  • 181
  • 1
  • 1
  • 3
  • @BDL: You linked to *this* question. Did you intend to link to [this one](https://stackoverflow.com/q/61243646/2675154) instead? – honk Apr 26 '20 at 11:00
  • @luke-tolley I've provided one solution which may be useful to you on this [similiar question](https://stackoverflow.com/q/61243646/2675154) – Ankit Makwana May 04 '20 at 04:24
  • check this and it will help you [Here](https://stackoverflow.com/a/36123751/11404554) – mahdi ashori Apr 11 '22 at 13:13

1 Answers1

1

The error message "DOMException: write permission denied" typically indicates that the WebView is not allowing access to the clipboard. To enable clipboard access in a WebView on Android, you need to add the android.permission.WRITE_EXTERNAL_STORAGE permission to your app's manifest file and then explicitly grant the permission at runtime.

Here's how to request the permission at runtime:

// Check if the permission has been granted
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
        != PackageManager.PERMISSION_GRANTED) {
    // Request the permission
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
            1);
} else {
    // Permission has already been granted
}

You'll also need to handle the result of the permission request:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
        int[] grantResults) {
    if (requestCode == 1) {
        if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Permission has been granted
        } else {
            // Permission has been denied
        }
    }
}

Note that requesting the WRITE_EXTERNAL_STORAGE permission may prompt the user to grant other permissions related to storage access, such as READ_EXTERNAL_STORAGE.