0

When an NFC tag is being read by the Web NFC API through Android WebView, the following error is shown:

NotAllowedError: Failed to execute 'scan' on 'NDEFReader': NFC permission request denied.

JavaScript in the Android WebView is enabled. I've seen in the Web NFC API documentation that Web NFC is supported in Android WebView.

MainActivity.kt code:

package com.example.webloader

import android.annotation.SuppressLint
import android.os.Bundle
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    private lateinit var webView: WebView
    private lateinit var urlInput: EditText
    private lateinit var goButton: Button

    @SuppressLint("SetJavaScriptEnabled")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        webView = findViewById(R.id.webView)
        urlInput = findViewById(R.id.urlInput)
        goButton = findViewById(R.id.goButton)

        webView.webViewClient = WebViewClient()
        webView.settings.javaScriptEnabled = true

        goButton.setOnClickListener {
            var url = urlInput.text.toString().trim()
            if (!url.startsWith("http://") && !url.startsWith("https://")) {
                url = "https://$url"
            }
            webView.loadUrl(url)
        }
    }
}

Is there any way to solve this?

5
  • Is the URL of the webview a secure context? i.e. https? as WebNFC only works in a secure context (w3c.github.io/web-nfc/#security-policies) Commented Nov 11 at 22:33
  • The url of the webview is secure, https. I've also check the url in chrome android, it's working fine. I also checked googlechrome.github.io/samples/web-nfc in my app, "nfc permission request denied " error is shown, but this link also works in chrome android. Commented Nov 12 at 1:24
  • 1
    I would show your AndroidManifest.xml as additional permissions are required Commented Nov 12 at 3:24
  • I've added "<uses-permission android:name="android.permission.NFC" /> <uses-feature android:name="android.hardware.nfc" android:required="true" />" in AndroidManifest.xml, but the "NFC permission request denied." still persists Commented Nov 16 at 7:05
  • Seems like a duplicate of Use Web NFC on Android Webview? as the answer links to a Chrome status page where at the bottom there is no entry for Android WebView support. Commented Nov 17 at 6:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.