19

I'm upgrading my app, actually it has sdk 28, and I need to upgrade to 29 in order to be allowed to create new versions in November.

sdk 30 it's available, so I try with this. The app works well, the only thing that I found, was that when I try to load in a webview a local html file, the webview show me an error:

  • net:err_access_denied with code -1

If I change the sdk version from 30 to 29, it "magically" works.

I has to mention that this file html is downloaded from a server, but when I try to open in a webview, it exists, and all it's apparently correct. I can see with other app like es file explorer.

¿There are some limitations with this new sdk and local html files in webview?

danybarco
  • 308
  • 1
  • 2
  • 9

6 Answers6

33

I had the same issue and it seems related to the latest changes applied to the webview in Android 11: https://developer.android.com/reference/android/webkit/WebSettings#setAllowFileAccess(boolean)

Now you will have to call setAllowFileAccess(true) in the settings of the webview to make it work. The default value was true for sdk 29 and lower versions, but now you will have to allow the access yourself.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Aitor Font Puig
  • 331
  • 2
  • 2
  • Yes. This is the right answer and worked for me. Don't know why google change structure. – SWIK Sep 26 '21 at 19:05
  • I tried this and the other suggestions, below, and almost have it working in a NativeScript 6.8 JavaScript project - see https://stackoverflow.com/questions/69623445/nativescript-webview-on-android-api-30-yields-err-access-denied – David Oct 19 '21 at 13:17
8

For NativeScript add

androidSettings.setAllowFileAccess(true);
androidSettings.setAllowContentAccess(true);

to _setAndroidWebViewSettings in the index.android.js file of nativescript-webview-interface folder

Nana kTk
  • 221
  • 3
  • 4
4

To piggyback on Nana's awesome answer, you dont need to modify the plugin code, as doing that will blow your change away every time you npm i. Do this instead:

webviewLoaded(args) {
  const view = args.object;
  if (view.android) {
    view.android.getSettings().setAllowFileAccess(true);
    view.android.getSettings().setAllowContentAccess(true);
  }
}

^ That is the true beauty of Nativescript.

davecoffin
  • 509
  • 1
  • 3
  • 11
3

I had the same issue, here is how I fixed using Kotlin

webview.settings.allowFileAccess = true
webview.settings.allowContentAccess = true
2

We had the same issue for our hybrid app in cordova.

Adding the following code to the file SystemWebViewEngine.java worked for us:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
        settings.setAllowFileAccess(true);
        settings.setAllowContentAccess(true);
}

Thanks to your answers we were able to find ours!

Victor York
  • 1,621
  • 4
  • 20
  • 55
0

The better way may be to use the new WebViewAssetLoader class - https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader . This allows your file: URIs to be hidden under a special https:// URL which is intercepted, which seems to be Google/WebKit's new way of providing secure access.